How to find the intersection of nested lists in Python

The intersection of a list and a nested list in a list containing sublists of elements from the intersection of the list and each sublist in the nested list. For example, the intersection of [1, 2, 3, 4, 5, 6] and [[1, 9, 2], [3, 2, 9], [5, 9, 9]] results in [[1, 2], [3, 2], [5]].

Solution for How to find the intersection of nested lists in Python : You can use a list comprehension, filter(), and a lambda expression to find the intersection of nested lists Use a list comprehension to loop over each sublist in a nested list. In the loop, use filter(function, iterable) with function as the lambda expression, lambda item: item in source_list and the sublist as the iterable. The lambda function will check if each item in the iterable is in the source_list. filter(function, iterable) returns an iterable, call list(object) to return the result of the previous step as a list.


how-to-find-the-intersection-of-nested-lists-in-python