How do u return a list that contains elements that are similar in both list | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How do u return a list that contains elements that are similar in both list

a=[1,3,4,5,6] b=[1,4,5]

13th Nov 2020, 11:26 AM
Simbarashe Mpofu
Simbarashe Mpofu - avatar
2 Answers
+ 7
As an alternative to the provided solution from NotAPythonNinja you could use: c = list(set(a) & set(b))
13th Nov 2020, 12:38 PM
_cm_
+ 6
2 perfect answers are already show, but i want to add a for loop. May be it helps for better understanding. a=[1,3,4,5,6] b=[1,4,5] c = [] for num in a: if num in b: c.append(num) print(c)
13th Nov 2020, 3:46 PM
Lothar
Lothar - avatar