I have this code:
list1 = [1,2,3]
list2 = [3,4,5]
list3 = []
for i, j in zip(list1,list2):
if i==j:
list3 = i,j
How can I make list3
store elements that are the same between list1
and list2
?
I have this code:
list1 = [1,2,3]
list2 = [3,4,5]
list3 = []
for i, j in zip(list1,list2):
if i==j:
list3 = i,j
How can I make list3
store elements that are the same between list1
and list2
?
An easy way using Python's set intersection
:
list1 = [1,2,3]
list2 = [3,4,5]
list3 = list(set(list1).intersection(list2))
print(list3)
or using a for loop
:
list1 = [1,2,3]
list2 = [3,4,5]
list3 = []
for i in list1:
if i in list2:
list3.append(i)
print(list3)