Python | Intersection of two lists
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.
Examples:
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]
Method 1:
This is the simplest method where we haven’t used any built-in functions.
# Python program to illustrate the intersection
# of two lists in most simple way
def intersection(lst1, lst2):
lst3 = [value for value in lst1 if value in lst2]
return lst3
# Driver Code
lst1 = [4, 9, 1, 17, 11, 26, 28, 54, 69]
lst2 = [9, 9, 74, 21, 45, 11, 63, 28, 26]
print(intersection(lst1, lst2))
Output:
[9, 11, 26, 28]
Method 2:
This method includes the use of set() method.
# Python program to illustrate the intersection
# of two lists using set() method
def intersection(lst1, lst2):
return list(set(lst1) & set(lst2))
# Driver Code
lst1 = [15, 9, 10, 56, 23, 78, 5, 4, 9]
lst2 = [9, 4, 5, 36, 47, 26, 10, 45, 87]
print(intersection(lst1, lst2))
Output:
[9, 10, 4, 5]The time complexity of the above program is O(n), where n is the length of the longer list between lst1 and lst2.
The space complexity of the program is O(n), where n is the length of the smaller list between lst1 and lst2.
Method 3:
In this method we set() the larger list and then use the built-in function called intersection() to compute the intersected list. intersection() is a first-class part of set.
# Python program to illustrate the intersection
# of two lists using set() and intersection()
def Intersection(lst1, lst2):
return set(lst1).intersection(lst2)
# Driver Code
lst1 = [ 4, 9, 1, 17, 11, 26, 28, 28, 26, 66, 91]
lst2 = [9, 9, 74, 21, 45, 11, 63]
print(Intersection(lst1, lst2))
Output:
{9, 11}
Method 4:
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.
# Python program to illustrate the intersection
# of two lists
def intersection(lst1, lst2):
# Use of hybrid method
temp = set(lst2)
lst3 = [value for value in lst1 if value in temp]
return lst3
# Driver Code
lst1 = [9, 9, 74, 21, 45, 11, 63]
lst2 = [4, 9, 1, 17, 11, 26, 28, 28, 26, 66, 91]
print(intersection(lst1, lst2))
Output:
[9, 9, 11]
Method 5:
This is the where the intersection is performed over sub-lists inside other lists. Here we have used the concept of filter().
# Python program to illustrate the intersection
# of two lists, sublists and use of filter()
def intersection(lst1, lst2):
lst3 = [list(filter(lambda x: x in lst1, sublist)) for sublist in lst2]
return lst3
# Driver Code
lst1 = [1, 6, 7, 10, 13, 28, 32, 41, 58, 63]
lst2 = [[13, 17, 18, 21, 32], [7, 11, 13, 14, 28], [1, 5, 6, 8, 15, 16]]
print(intersection(lst1, lst2))
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.
Output:
[[13, 32], [7, 13, 28], [1, 6]]Method 6: Using reduce():
Algorithm:
- Import the reduce function from functools module.
- Define two lists.
- Initialize the variable intersection with an empty list.
- Use the reduce function to iterate over the elements of lst1.
- Inside the lambda function, check if the current element is present in lst2 and not already present in the intersection list.
- If it is, then add the current element to the intersection list.
- Return the intersection list.
- Print the intersection list.
from functools import reduce
lst1 = [15, 9, 10, 56, 23, 78, 5, 4, 9]
lst2 = [9, 4, 5, 36, 47, 26, 10, 45, 87]
intersection = reduce(lambda acc, x: acc + [x] if x in lst2 and x not in acc else acc, lst1, [])
print(intersection)
#This code is contributed by Rayudu.
Output
[9, 10, 5, 4]
Time Complexity: O(n^2), where n is the length of lst1.
Space Complexity: O(n), where n is the length of lst1.
Python | Intersection of two lists – FAQs
How Do You Use Two Lists in Python?
Using two lists in Python can be done in various ways depending on the task at hand. Common operations include iterating over both lists simultaneously, combining them, or performing set operations. For simultaneous iteration, you can use the
zip()function, which pairs elements from two lists together in a tuple for each iteration.Example of Using
zip():list1 = [1, 2, 3]
list2 = ['a', 'b', 'c']
for number, letter in zip(list1, list2):
print(f"{number}: {letter}")
How to Create a Set from Two Lists in Python?
To create a set from two lists, which effectively combines the elements of both lists and removes duplicates, you can use set operations.
Example:
list1 = [1, 2, 3, 4]
list2 = [3, 4, 5, 6]
combined_set = set(list1).union(list2)
print(combined_set) # Output: {1, 2, 3, 4, 5, 6}
How Do You Check if Two Lists Intersect in Python?
To check if two lists intersect (i.e., have common elements), you can use set operations. Here’s how you can find the intersection of two lists:
Example:
list1 = [1, 2, 3, 4]
list2 = [3, 4, 5, 6]
intersection = set(list1).intersection(list2)
if intersection:
print("The lists intersect.")
else:
print("The lists do not intersect.")
How to Take Two Lists as Input in Python?
To take two lists as input in Python, you can prompt the user to enter the elements separated by a space and then use the
split()method to convert the inputs into lists.Example:
list1 = input("Enter elements of the first list separated by space: ").split()
list2 = input("Enter elements of the second list separated by space: ").split()
print("List 1:", list1)
print("List 2:", list2)
How Do You Write Multiple Lists in Python?
Writing multiple lists in Python can refer to creating multiple list variables or writing them to a file. If you mean creating them, simply define each list with its elements. If you mean writing to a file, you can iterate through each list and write its elements to a file.
Example of Writing Lists to a File:
list1 = [1, 2, 3]
list2 = [4, 5, 6]
with open('lists.txt', 'w') as file:
for item in list1:
file.write(f"{item}\n")
for item in list2:
file.write(f"{item}\n")
Enhance your coding skills with DSA Python, a comprehensive course focused on Data Structures and Algorithms using Python. Over 90 days, you'll explore essential algorithms, learn how to solve complex problems, and sharpen your Python programming skills. This course is perfect for anyone looking to level up their coding abilities and get ready for top tech interviews.
Join the Three 90 Challenge and commit to completing 90% of the course in 90 days to earn a 90% refund. It’s the perfect way to stay focused, track your progress, and reap the rewards of your hard work. Take the challenge and become a DSA expert in Python today


