Python intersection of two lists | Example code

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.1.3 (Community Edition)

Windows 10

Python 3.7

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


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.