Categories
Python Answers

How to get the intersection of multiple lists with Python?

Spread the love

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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

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