Explain Codes LogoExplain Codes Logo

Find intersection of two nested lists?

python
set-operations
functional-programming
performance-optimization
Anton ShumikhinbyAnton Shumikhin·Sep 17, 2024
TLDR

The quickest recipe to intersect two nested lists is using a simple list comprehension:

nested_list1 = [[1, 2], [3, 4]] nested_list2 = [[3, 4], [5, 6]] # We are making the Python version of grilled cheese - simple yet delicious! intersection = [item for item in nested_list1 if item in map(tuple, nested_list2)] print(intersection) # Mmm... grilled cheese: [[3, 4]]

Intersection is achieved by selecting those sublists from nested_list1 that also make it to the guest list in nested_list2. This method is a swift operator when catering for small lists with hashable elements.

Work with sets for large operations

For larger nested lists, our kitchen needs a better chef - set operations. They can reduce your dish preparation time significantly. Don't believe me? Let's see:

nested_list1 = [[1, 2], [3, 4], [5, 6]] nested_list2 = [[3, 4], [5, 6], [7, 8]] # Welcome to the big leagues! intersection = set(map(tuple, nested_list1)).intersection(map(tuple, nested_list2)) print(intersection) # WoW that was fast! : {(3, 4), (5, 6)}

Making unhashable items work with frozenset

When dealing with unhashable items, like the infamous nested lists, we can leverage frozenset to the rescue:

nested_list1 = [[1, 2], [3, 4], [5, 6]] nested_list2 = [[3, 4], [5, 6], [7, 8]] # FroZone to the rescue! intersection = [item for item in map(frozenset, nested_list1) if item in map(frozenset, nested_list2)] print(intersection) # Honey, where is my super suit? : [frozenset({3, 4}), frozenset({5, 6})]

This approach maintains order and holds back mutable properties from spoiling our operation.

Embrace functional programming with reduce

Functional programming concepts such as reduce provide powerful tools for intersecting multiple nested lists:

from functools import reduce nested_list1 = [[1, 2], [3, 4], [5, 6]] nested_list2 = [[3, 4], [5, 6], [7, 8]] nested_list3 = [[5, 6], [9, 10], [3, 4]] # Reduce, Reuse, Recycle for cleaner code and environment! intersection = reduce(lambda acc, lst: acc & set(map(tuple, lst)), [nested_list1, nested_list2, nested_list3], set(map(tuple, nested_list1))) print(intersection) # Green Code! : {(3, 4), (5, 6)}

Here, reduce joins forces with set and lambda to perform intersection in a clean, efficient and eco-friendly manner.

Fine-tuning performance with hybrid methods

Combining the best of lists and sets, hybrid methods juggle readability with performance:

# Welcome to the Hybrid Theory! intersection = [item for item in nested_list1 if {tuple(item)} & {tuple(subl) for subl in nested_list2}] # Yes! Both Park and Linkin would be proud!

Being mindful of corner cases

Here's a roundup of few corner cases:

  1. Immutability: Nested lists containing mutable elements need to be converted to immutable types.
  2. Duplicates: Set operations will eliminate duplicates. Be aware of this side-effect.
  3. Order: If order matters, make sure you handle it post intersection operation as sets do not maintain order.