Menu Close

How to get the intersection of multiple lists with Python?

Sometimes, we want to get the intersection of multiple lists with Python.

In this article, we’ll look at how to get the intersection of multiple lists with Python.

How to get the intersection of multiple lists with Python?

To get the intersection of multiple lists with Python, we call set.intersection.

For instance, we write

intersection = set.intersection(*map(set, d))

to call set.intersection with arguments being the sets that we get from

*map(set, d)

We call map with set and list d to convert the lists in list d to sets.

Then we use * to expand the iterator with sets as arguments of intersection.

Conclusion

To get the intersection of multiple lists with Python, we call set.intersection.

Posted in Python, Python Answers