get the intersection of two lists python code example
Example 1: intersection of two lists python
>>> a = [1,2,3,4,5]
>>> b = [1,3,5,6]
>>> list(set(a) & set(b))
[1, 3, 5]
Example 2: python intersection of two lists
def intersection(lst1, lst2):
lst3 = [value for value in lst1 if value in lst2]
return lst3
lst1 = [4, 9, 1, 17, 11, 26, 28, 54, 69]
lst2 = [9, 9, 74, 21, 45, 11, 63, 28, 26]
print(intersection(lst1, lst2))
Example 3: intersection of lists in python
import numpy as np
recent_coding_books = np.intersect1d(recent_books,coding_books)
Example 4: list intersection python
name1 =list("".join(str(x)for x in input("Enter name1").replace(" ","")))
name2 =list("".join(str(x)for x in input("Enter name2").replace(" ","")))
common = [x for x in name1 if x in name2]
unique = set(common)
d=0
for x in unique:
d = d + min(name1.count(x),name2.count(x))
difference = (len(name1) + len(name2)) - d*2
print(difference)