Given two list of lists, write a Python program to find the intersection between the given two lists.
Input : lst1 = [['a', 'c'], ['d', 'e']] lst2 = [['a', 'c'], ['e', 'f'], ['d', 'e']] Output : [['a', 'c'], ['d', 'e']] Input : lst1 = [[1, 5, 7], [2, 3], [6, 9], [4, 8]] lst2 = [[9, 3], [2, 3], [6, 9]] Output : [[2, 3], [6, 9]]
Approach #1 : Naive(List comprehension)
The brute-force or naive approach to find the intersection of list of lists is to use List comprehension or simply a for loop.
[['a', 'c'], ['d', 'e']]
Approach #2 : Using Set intersection()
This is an efficient method in comparison to the naive approach. We first convert both list of lists into list of tuples using map() because Python sets are compatible with tuples, not lists. Then we simply find Set intersection() of both the lists.
[['d', 'e'], ['a', 'c']]
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
- Python | Intersection of two lists
- Python | Zipping two lists of lists
- Python | Program to count number of lists in a list of lists
- Python - Convert Lists into Similar key value lists
- Python Iterate over multiple lists simultaneously
- Python | Interleave multiple lists of same length
- Python | Initializing multiple lists
- Python | Append multiple lists at once
- Python - Elements frequency count in multiple lists
- Python counter and dictionary intersection example (Make a string using deletion and rearrangement)
- Python set operations (union, intersection, difference and symmetric difference)
- Intersection of two arrays in Python ( Lambda expression and filter function )
- Intersection() function Python
- Python | Find common elements in three sorted arrays by dictionary intersection
- Python | Intersection of two String
- Python | Intersection of two nested list
- Python | Pandas TimedeltaIndex.intersection
- Python | Pandas Index.intersection()
- Python | Intersection in Tuple Records Data
- Python | Records Intersection
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.