Intersection of two list means we need to take all those elements which are common to both of the initial lists and store them into another list. Now there are various ways in Python, through which we can perform the Intersection of the lists.
Input : lst1 = [15, 9, 10, 56, 23, 78, 5, 4, 9] lst2 = [9, 4, 5, 36, 47, 26, 10, 45, 87] Output : [9, 10, 4, 5] Input : lst1 = [4, 9, 1, 17, 11, 26, 28, 54, 69] lst2 = [9, 9, 74, 21, 45, 11, 63, 28, 26] Output : [9, 11, 26, 28]
This is the simplest method where we haven’t used any built-in functions.
[9, 11, 26, 28]
This method includes the use of set() method.
[9, 10, 4, 5]
In this method we set() the larger list and then use the built-in function called interscetion() to compute the intersected list. intersection() is a first-class part of set.
By the use of this hybrid method the complexity of the program falls to O(n). This is an efficient way of doing the following program.
[9, 9, 11]
This is the where the intersection is performed over sub-lists inside other lists. Here we have used the concept of filter().
Working: The filter part takes each sublist’s item and checks to see if it is in the source list. The list comprehension is executed for each sublist in list2.
[[13, 32], [7, 13, 28], [1, 6]]
- Python | Intersection of multiple lists
- Python | Program to count number of lists in a list of lists
- Intersection() function Python
- Python | Intersection of two String
- Python | Pandas TimedeltaIndex.intersection
- Python | Intersection of two nested list
- Python | Pandas Index.intersection()
- Python | Zipping two lists of lists
- Intersection of two arrays in Python ( Lambda expression and filter function )
- Python | Find common elements in three sorted arrays by dictionary intersection
- Python counter and dictionary intersection example (Make a string using deletion and rearrangement)
- Python set operations (union, intersection, difference and symmetric difference)
- Python | Union of two or more Lists
- Python | Union of Value Lists
- Python | All possible permutations of N lists
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.