How to get the difference between the two lists in Python

2 Answers

0 votes
listA = [4, 5, 7, 8, 1, 2, 9, 9, 10, 12, 19]
listB = [4, 2, 1, 3, 4, 8, 7, 9, 11,  3,  4, 17]

setA = set(listA)
setB = set(listB)

list_diff = list(setA - setB)

print(list_diff)





'''
run:

[10, 19, 12, 5]

'''

 



answered Apr 19, 2021 by avibootz
0 votes
listA = [4, 5, 7, 8, 1, 2, 9, 9, 10, 12, 19]
listB = [4, 2, 1, 3, 4, 8, 7, 9, 11,  3,  4, 17]

list_diff = [element for element in listA if element not in listB]

print(list_diff)





'''
run:

[5, 10, 12, 19]

'''

 



answered Apr 19, 2021 by avibootz

Related questions

3 answers 288 views
1 answer 105 views
1 answer 169 views
1 answer 152 views
1 answer 164 views
...