There are many ways to find the common elements in two lists. Some of them are mentioned below :
>>> a=[i for i in range (5,10)] >>> a [5, 6, 7, 8, 9] >>> b=[i for i in range(7,15)] >>> b [7, 8, 9, 10, 11, 12, 13, 14] >>> common=[c for c in a if c in b] >>> common [7, 8, 9] >>>
Here both the lists a and b has common elements [7,8,9] .
>>> c= list(set(a) & set(b)) >>> c [8, 9, 7] >>>
>>> c=list(set(a).intersection(b)) >>> c [8, 9, 7] >>>
This article is contributed by Anmol. If you like dEexams.com and would like to contribute, you can write your article here or mail your article to admin@deexams.com . See your article appearing on the dEexams.com main page and help others to learn.