How to compare two lists in python and return the matches (common elements) in Python

2 Answers

0 votes
l1 = [1, 2, 3, 4, 5]
l2 = [6, 7, 8, 9, 5, 4]

r = set(l1) & set(l2)

print(r)


'''
run:

{4, 5}

'''

 



answered Nov 4, 2017 by avibootz
0 votes
l1 = [1, 2, 3, 4, 5]
l2 = [6, 7, 8, 9, 5, 4]

result = set(l1).intersection(l2)

for n in result:
    print(n, end=" ")


'''
run:

4 5

'''

 



answered Nov 4, 2017 by avibootz
...