consider two lists a=[1,2,3], b=[1,4,5].The Code should print the similar values c=[1] and the Code should print d=[2,3,4,5] which shows different values
--completed --
consider two lists a=[1,2,3], b=[1,4,5].The Code should print the similar values c=[1] and the Code should print d=[2,3,4,5] which shows different values
--completed --
A simple and alternative solution using list comprehension and logical operators is given below:
a = [1,2,3]
b = [1,4,5]
print([x for x in a if x in b])
print([x for x in set(a+b) if (x in a) ^ (x in b)])
A solution with set operation
has already been given by other person, so I am not repeating it here.
You can use sets for this
a = [1,2,3]
b = [1,4,5]
c = list(set(a).intersection(b))
d = list(set(a).difference(b)) + list(set(b).difference(a))
intersection
finds the common elements and difference
finds what is different. You have to do this both ways because the difference
operator is basically showing only the elements in the first set that aren't in the second.