How can I find the intersection of two lists in python? I've tried it with the in operator but am unsure as to how I do it without.
a = [2, 4, 6, 8, 10]
b = [4, 8, 12, 16, 20]
set(a) & set(b)
This should return [4, 8]
How can I find the intersection of two lists in python? I've tried it with the in operator but am unsure as to how I do it without.
a = [2, 4, 6, 8, 10]
b = [4, 8, 12, 16, 20]
set(a) & set(b)
This should return [4, 8]
So you can get your intersection by using the example like below.
Intersection is already a first class part of set, you can directly use it
a = [2, 4, 6, 8, 10]
b = [4, 8, 12, 16, 20]
set(a).intersection(b)
The sets module provides classes for constructing and manipulating unordered collections of unique elements.Computing standard math operations on sets such as intersection, union etc.