1def intersection(lst1, lst2):
2 lst3 = [value for value in lst1 if value in lst2]
3 return lst3
4
5# Driver Code
6lst1 = [4, 9, 1, 17, 11, 26, 28, 54, 69]
7lst2 = [9, 9, 74, 21, 45, 11, 63, 28, 26]
8print(intersection(lst1, lst2))
1a = ['apple', 'banana', 'pear']
2b = ['fridge', 'stove', 'banana']
3
4a & b == ['banana'] #True
1# intersection of two lists (lst1 & lst2)
2In [1]: x = ["a", "b", "c", "d", "e"]
3
4In [2]: y = ["f", "g", "h", "c", "d"]
5
6In [3]: set(x).intersection(y)
7Out[3]: {'c', 'd'}
8# has_intersection = bool(set(x).intersection(y)) -> True
1#get name1 and name2 as a list input
2name1 =list("".join(str(x)for x in input("Enter name1").replace(" ","")))
3name2 =list("".join(str(x)for x in input("Enter name2").replace(" ","")))
4#check using list comprehension if x in name1 is in name2
5#this will return multiple instances of the same character from name1 that matches with the name2
6common = [x for x in name1 if x in name2]
7#create a set out of the output so as to have only unique values of the repeated characters
8unique = set(common)
9#thus the above set will have common unrepeated characters from both names
10#create a variable and initialize it to zero
11d=0
12#run a loop that checks the minimum occurrence of
13#the character from the set in name1 & name2
14#Minimum because for ex: a might exist thrice in name1, but only twice in name2
15#we will need to take only 2 common occurrences from the name2
16#thus finding the minimum occurrence of the character from both names
17for x in unique:
18 d = d + min(name1.count(x),name2.count(x))
19#multiplying by two, because if one character from name 1 matches with one,
20#character from name2, then it makes two in total
21difference = (len(name1) + len(name2)) - d*2
22print(difference)