Finding common values between two lists is easy in Python when compared to Javascript. First remove all None values. Because the in-built function will consider None also as a value to compare. If we don't want that then remove it using filter function. Then make one of the lists as set and use intersection function as follows:
Run the code here: https://repl.it/@VinitKhandelwal/CommonElementsList
Run the code here: https://repl.it/@VinitKhandelwal/CommonElementsList
list1 = ['a','b','c',None]
list2 = ['c','d','e',None]
list3 = list(filter(lambda x: x is not None, list1))
list4 = list(filter(lambda x: x is not None, list2))
print(list3)
print(list4)
print(list(set(list3).intersection(list4)))
list1 = [1,2,3,4,None,5,6]
list2 = [3, 5, 7, 9, None]
list3=[]
for i in list1:
for j in list2:
if i is not None and i == j:
list3.append(i)
break
print(f"{list3} - function to find common values")
OUTPUT
[3, 5] - function to find common values
['a', 'b', 'c']
['c', 'd', 'e']
['c']
Comments
Post a Comment