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 | Zipping two lists of lists
- Intersection of two arrays in Python ( Lambda expression and filter function )
- Python | Intersection of two String
- Python | Intersection of two nested list
- Intersection of two dataframe in Pandas - Python
- Python | Program to count number of lists in a list of lists
- Python - Convert Lists into Similar key value lists
- Python counter and dictionary intersection example (Make a string using deletion and rearrangement)
- Python set operations (union, intersection, difference and symmetric difference)
- Intersection() function Python
- Python | Find common elements in three sorted arrays by dictionary intersection
- Python | Pandas TimedeltaIndex.intersection
- Python | Pandas Index.intersection()
- Python | Intersection in Tuple Records Data
- Python | Records Intersection
- Python | Sympy Line.intersection() method
- python | Nested List Intersection Matrix Product
- Python - Rows intersection with K
- Python – Sympy Polygon.intersection() Method