A=['mumbai','delhi','jaipur']
B=['mumbai','kolkata','pune']
I want to compare list A and list B and if list B cities are not in list A then we should able to append those cities in list A
CodePudding user response:
One more way to do the same but not by using the for loop.
difference_between_lists = list(set(B) - set(A))
A = difference_between_lists
CodePudding user response:
You loop through list B and check if the item in list B is in list A. If it is, don't do anything, if it isn't, append it to list A.
for i in B:
if not i in A:
A.append(i)
CodePudding user response:
You can also do this using list comprehension
C = [i for i in B if not i in A]
print(A C)
CodePudding user response:
You can also use sets and do the union.
setA = set(A)
setB = set(B)
print(setB.union(setA))