How to find common elements (e.g. intersection) between two lists in python ?

Published: September 21, 2021

Tags: Python; List;

DMCA.com Protection Status

Examples of how to find common elements between two lists in python:

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.

References