def intersection(list1 list2) code example
Example 1: intersection in list
def intersection(lst1, lst2):
lst3 = [value for value in lst1 if value in lst2]
return lst3
# Driver Code
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 2: python check if two lists intersect
a = [1, 2, 3]
b = [3, 4, 5]
bool(set(a) & set(b))
Example 3: python list INTERSECTION
a = ['apple', 'banana', 'pear']
b = ['fridge', 'stove', 'banana']
a & b == ['banana'] #True