[solved](python)Shortcut to express list1-list2 ? | Sololearn: Learn to code for FREE!
Novo curso! Todo programador deveria aprender IA generativa!
Experimente uma aula grƔtis
+ 3

[solved](python)Shortcut to express list1-list2 ?

I have 2 lists: list1 = [1,2,3,4] list2 = [3,4] If I want to prevent the numbers in list2 to be shown in list1, is there a shortcut for that? Or I have to write long code like below: list1 = [1, 2, 3, 4] list2 = [3, 4] temp_list = [] i = 0 while i < len(list1): if list1[i] not in list2: temp_list.append(list1[i]) i += 1 list1 = temp_list print(list1)

9th Nov 2021, 11:30 AM
Lea
Lea - avatar
18 Respostas
+ 16
[i for i in list1 if i not in list2] https://code.sololearn.com/cbn7mwGiujgk/?ref=app
9th Nov 2021, 11:33 AM
Hatsy Rei
Hatsy Rei - avatar
+ 10
[edited] sorry, the lists sequence in the lambda was wrong. here the correct version: list1 = [1, 2, 3, 4] list2 = [3, 4] print(list(filter(lambda x:x not in list2, list1))) Lea , just for the sake of completeness: using filter with a lambda function can also do this job: list1 = [1, 2, 3, 4] list2 = [3, 4] print(list(filter(lambda x:x not in list1, list2))) # not correct!
9th Nov 2021, 7:00 PM
Lothar
Lothar - avatar
+ 7
print(list(set(list1) - set(list2)))
9th Nov 2021, 11:34 AM
Simba
Simba - avatar
+ 7
Lea oh no, Converting lists into sets has some pitfalls. Just add a 2nd element 1 to list1. It will disappear after converting to set. Simba s solution is cool.Hatsy Rei s solution is safe.
9th Nov 2021, 11:49 AM
Oma Falk
Oma Falk - avatar
+ 5
the modules name to import is 'collections' not 'collection' ^
12th Nov 2021, 4:44 PM
Lothar
Lothar - avatar
+ 4
Hatsy Rei Simba Both codes works! Thank you!
9th Nov 2021, 11:39 AM
Lea
Lea - avatar
+ 4
Oma Falk I just tried! You are right! Thank you for pointing it out! https://code.sololearn.com/cGX90sT9QgaI/?ref=app
9th Nov 2021, 12:01 PM
Lea
Lea - avatar
+ 4
Prabhas Koya Thank you for providing more solutions! By the way, Is ā€œcollectionā€ a moduler i need to add to my library before I run the code?
11th Nov 2021, 11:14 PM
Lea
Lea - avatar
+ 3
Simba This is so cool! It means I can apply all the mathematical operations for sets on lists!
9th Nov 2021, 11:43 AM
Lea
Lea - avatar
+ 3
I wonder why lists donā€™t have mathematical opertations like sets. Sets and lists, it seems both of them are for holding data, but their methods are so differentā€¦..
9th Nov 2021, 12:07 PM
Lea
Lea - avatar
+ 3
for i in list2: try: list1.remove(i) except: pass ''' Looping only through list2, I think this is efficient. Hatsy Rei is looping through extra items in list1 . IF YOU KNOW YOUR LIST2 CONTAINS ELEMENTS WHICH ARE DEFINITELY IN LIST1 , THEN SIMPLY for i in list2: list1.remove(i) ''' making more faster import collections list1 = collections.deque([1,1,2,3]) list2 = [3,4] for i in list2: try: list1.remove(i) except: pass
11th Nov 2021, 7:37 AM
Prabhas Koya
+ 2
ā˜ŗļø
9th Nov 2021, 1:04 PM
Lea
Lea - avatar
+ 2
Hi, Lothar I ran you code and get the result below: []
10th Nov 2021, 12:23 AM
Lea
Lea - avatar
+ 2
Yes , you need to import before running your code.
12th Nov 2021, 3:52 AM
Prabhas Koya
+ 1
Here it goes list1 = [1, 1, 2, 3, 4]; list2 = [3, 4] list0 = list(set(list1 + list2)); print(list0)
10th Nov 2021, 9:51 AM
Pee Frosh
Pee Frosh - avatar
11th Nov 2021, 3:09 AM
Ally Ahmed
Ally Ahmed - avatar
+ 1
Lothar yeah that was a typo thanks for correcting
13th Nov 2021, 4:15 AM
Prabhas Koya
0
I need your help guys to improve my code
11th Nov 2021, 3:09 AM
Ally Ahmed
Ally Ahmed - avatar