In Python, the easiest way to get the intersection of two lists is by using list comprehension to identify the values which are in both lists.
list_1 = [5,3,8,2,1]
list_2 = [9,3,4,2,1]
intersection_of_lists = [x for x in list_1 if x in list_2]
print(intersection_of_lists)
#Output:
[3, 2, 1]
If you want to find the intersection of more than two lists, we can easily do that in Python with a simple function.
def intersection(lists):
all_elements = lists[0]
for i in range(1,len(lists)):
all_elements = [x for x in all_elements if x in lists[i]]
return all_elements
list1 = [3,2,1,8,5,3,1]
list2 = [9,5,6,3,4,2]
list3 = [1,0,9,2,8,5,4]
print(intersection([list1,list2,list3]))
#Output:
[2,5]
When working with multiple lists, it can be useful to find the entire collection of elements which exist in all of your lists. The intersection of two or more lists is a list of elements which are included in all of the lists.
So, for example, if we have a list A and a list B, then the intersection of A and B is a list of elements which are in both A and B.
We can get the intersection of two lists in Python easily.
To get the intersection of lists in Python, we can use list comprehension to identify the values which are in both lists.
Below is an example of how to get the intersection of two lists in Python.
list_1 = [5,3,8,2,1]
list_2 = [9,3,4,2,1]
intersection_of_lists = [x for x in list_1 if x in list_2]
print(intersection_of_lists)
#Output:
[3, 2, 1]
If you’d like a result which is sorted, you can use the sorted() function or sort() function.
list_1 = [5,3,8,2,1]
list_2 = [9,3,4,2,1]
intersection_of_lists = [x for x in list_1 if x in list_2]
print(sorted(intersection_of_lists))
#Output:
[1,2,3]
If you’d instead like to get the union of multiple lists, you can read our article about finding the union of lists in Python.
Finding the Intersection of Lists by Converting to Sets Python
The Python set data structure has many great capabilities for finding the union or intersection of multiple sets. We can convert our lists to sets and then use set functions to find the intersection of these lists.
Below is an example of using the intersection() set function to find the intersection of two lists in Python.
list_1 = [5,3,8,2,1]
list_2 = [9,3,4,2,1]
intersection_of_lists = list(set(list_1).intersection(list_2))
print(intersection_of_lists)
#Output:
[1,2,3]
Another way you can find the intersection of two lists is by converting the lists to sets, and then using the & Python operator.
Below is another example in Python of how to find the intersection of two lists.
list_1 = [5,3,8,2,1]
list_2 = [9,3,4,2,1]
intersection_of_lists = list(set(list_1) & set(list_2))
print(intersection_of_lists)
#Output:
[1,2,3]
How to Find the Intersection of More Than Two Lists in Python
So far in this article, we’ve only found the intersection of two lists. We can easily find the intersection of more than two lists in Python.
To do so, we can define a function which takes in a number of lists, and then finds the intersection of all of the lists using list comprehension.
Since intersection of a list has the associative law, we can iterate over the lists and take intersections between the intersection of all lists to that point, and the next list.
Below is an example of how to find the intersection of more than two lists in Python.
def intersection(lists):
all_elements = lists[0]
for i in range(1,len(lists)):
all_elements = [x for x in all_elements if x in lists[i]]
return all_elements
list1 = [3,2,1,8,5,3,1]
list2 = [9,5,6,3,4,2]
list3 = [1,0,9,2,8,5,4]
print(intersection([list1,list2,list3]))
#Output:
[2,5]
Hopefully this article has been useful for you to learn how to use Python to get the intersection of lists.