Examples of how to find common elements between two lists in python:
Table of contents
Create two lists in python
Let's create a list in python
list_a = ['e','0', 6, 'f', 4, 1, 'g']
and another
list_b = ['a','d', 1, 'o', 8, 3, 'e']
Find common elements between two lists
To find common elements between two lists, a solution is to use Python Set intersection() Method. To do so, let's first convert one of the two lists in a set:
list_a_as_set = set(list_a)
and then apply intersection
intersection = list_a_as_set.intersection(list_b)
for i in intersection:
print( i )
returns here
1
e
Note that:
type(intersection)
is a set.