Menu Close

How to find intersection of two nested lists with Python?

Sometimes, we want to find intersection of two nested lists with Python.

In this article, we’ll look at how to find intersection of two nested lists with Python.

How to find intersection of two nested lists with Python?

To find intersection of two nested lists with Python, we can call the set’s intersection method.

For instance, we write

b1 = [1, 2, 3, 4, 5, 9, 11, 15]
b2 = [4, 5, 6, 7, 8]
s = set(b1).intersection(b2)

to create a set from the b1 list.

And then we call intersection with the b2 list to return a set that has the elements in both lists.

Conclusion

To find intersection of two nested lists with Python, we can call the set’s intersection method.

Posted in Python, Python Answers