# Python | Intersection of two nested list

This particular article aims at achieving the task of intersecting two list, in which each element is in itself a list. This is also a useful utility as this kind of task can come in life of programmer if he is in the world of development. Lets discuss some ways to achieve this task.

Method 1: Naive Method This is the simplest method to achieve this task and uses the brute force approach of executing a loop and to check if one list contains similar list as of the other list.

## Python3

 `# Python 3 code to demonstrate ` `# find intersection of nested list` `# using naive method`   `# initializing lists` `test_list1 ``=` `[ [``1``, ``2``], [``3``, ``4``], [``5``, ``6``] ]` `test_list2 ``=` `[ [``3``, ``4``], [``5``, ``7``], [``1``, ``2``] ]`   `# printing both lists ` `print` `("The original ``list` `1` `: " ``+` `str``(test_list1))` `print` `("The original ``list` `2` `: " ``+` `str``(test_list2))`   `# using naive method ` `# to get list intersection` `res_list ``=` `[]` `for` `i ``in` `test_list1:` `    ``if` `i ``in` `test_list2:` `        ``res_list.append(i)`   `# printing the intersection ` `print` `("The intersection of two lists ``is` `: " ``+` `str``(res_list))`

Output :

```The original list 1 : [[1, 2], [3, 4], [5, 6]]
The original list 2 : [[3, 4], [5, 7], [1, 2]]
The intersection of two lists is : [[1, 2], [3, 4]]```

Time Complexity: O(n2)
Auxiliary Space: O(n)

Method 2: Using list comprehension We can perform the similar task as discussed above in comparatively less no. of lines. But the method is similar as above just uses list comprehension approach to perform the task.

## Python3

 `# Python 3 code to demonstrate ` `# find intersection of nested list` `# using list comprehension`   `# initializing lists` `test_list1 ``=` `[ [``1``, ``2``], [``3``, ``4``], [``5``, ``6``] ]` `test_list2 ``=` `[ [``3``, ``4``], [``5``, ``7``], [``1``, ``2``] ]`   `# printing both lists ` `print` `("The original ``list` `1` `: " ``+` `str``(test_list1))` `print` `("The original ``list` `2` `: " ``+` `str``(test_list2))`   `# using list comprehension` `# to get list intersection` `res_list ``=` `[i ``for` `i ``in` `test_list1 ``if` `i ``in` `test_list2]`   `# printing the intersection ` `print` `("The intersection of two lists ``is` `: " ``+` `str``(res_list))`

Output :

```The original list 1 : [[1, 2], [3, 4], [5, 6]]
The original list 2 : [[3, 4], [5, 7], [1, 2]]
The intersection of two lists is : [[1, 2], [3, 4]]```

Time Complexity: O(n2)
Auxiliary Space: O(n)

Method 3: Using set() + map() and & The most efficient and recommended method to perform this task is using the combination of set() and map() to achieve it. Firstly converting inner lists to tuples using map, and outer lists to set, use of & operator can perform the set intersection and hence perform this task. Further if it is required to get in lists of list fashion, we can convert outer and inner containers back to list using map().

## Python3

 `# Python 3 code to demonstrate ` `# find intersection of nested list` `# using map() + set() + &`   `# initializing lists` `test_list1 ``=` `[ [``1``, ``2``], [``3``, ``4``], [``5``, ``6``] ]` `test_list2 ``=` `[ [``3``, ``4``], [``5``, ``7``], [``1``, ``2``] ]`   `# printing both lists ` `print` `("The original ``list` `1` `: " ``+` `str``(test_list1))` `print` `("The original ``list` `2` `: " ``+` `str``(test_list2))`   `# using map() + set() + &` `# to get list intersection` `res_set ``=` `set``(``map``(``tuple``, test_list1)) & ``set``(``map``(``tuple``, test_list2))` `res_list ``=` `list``(``map``(``list``, res_set))`   `# printing the intersection ` `print` `("The intersection of two lists ``is` `: " ``+` `str``(res_list))`

Output :

```The original list 1 : [[1, 2], [3, 4], [5, 6]]
The original list 2 : [[3, 4], [5, 7], [1, 2]]
The intersection of two lists is : [[1, 2], [3, 4]]```

Time Complexity: O(n)
Auxiliary Space: O(n)

Method 4: Using filter() and lambda function

• Convert both nested lists into sets using map() and set().
• Create a lambda function that takes a nested list and returns True if it’s a subset of the second set.
• Use filter() with the lambda function to get a filter object of the subsets.
• Convert the filter object to a list using list().
• Print the resulting list.

## Python3

 `# Python 3 code to demonstrate ` `# find intersection of nested list` `# using filter() and lambda function`   `# initializing lists` `test_list1 ``=` `[ [``1``, ``2``], [``3``, ``4``], [``5``, ``6``] ]` `test_list2 ``=` `[ [``3``, ``4``], [``5``, ``7``], [``1``, ``2``] ]`   `# printing both lists ` `print` `(``"The original list 1 : "` `+` `str``(test_list1))` `print` `(``"The original list 2 : "` `+` `str``(test_list2))`   `# using filter() and lambda function` `# to get list intersection` `set1 ``=` `set``(``map``(``tuple``, test_list1))` `set2 ``=` `set``(``map``(``tuple``, test_list2))` `intersection_list ``=` `list``(``filter``(``lambda` `x: ``tuple``(x) ``in` `set2, test_list1))`   `# printing the intersection ` `print` `(``"The intersection of two lists is : "` `+` `str``(intersection_list))`   `#`

Output

```The original list 1 : [[1, 2], [3, 4], [5, 6]]
The original list 2 : [[3, 4], [5, 7], [1, 2]]
The intersection of two lists is : [[1, 2], [3, 4]]```

Time Complexity: O(n^2), where n is the length of the nested lists.
Auxiliary Space: O(k), where k is the size of the intersection list.

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Previous
Next