Skip to content

Python intersection of two lists | Example code

  • by

Use the set intersection() method to find the intersection of two lists in Python. First Convert the lists to sets using set(iterable) then use intersection() with these two sets to get the intersection.

Example intersection of two lists in Python

Simple example code finds the intersection of list1 and list2.

list1 = [1, 2, 3]
list2 = [1, 3, 5]

res = set.intersection(set(list1), set(list2))

print(list(res))

Output:

Python intersection of two lists

Do comment if you have any doubts or suggestions on this python Python intersection code.

Note: IDE: PyCharm 2021.3.3 (Community Edition)

Windows 10

Python 3.10.1

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.

Leave a Reply

Your email address will not be published. Required fields are marked *