Last updated: Apr 10, 2024
Reading timeยท5 min
To find the common values in multiple lists:
set
object.intersection()
method on the set
.intersection
method will return the common elements in the lists.list1 = ['a', 'b', 'c'] list2 = ['a', 'z', 'c'] list3 = ['a', 'x', 'c'] common_elements = list( set(list1).intersection(list2, list3) ) print(common_elements) # ๐๏ธ ['a', 'c']
If you need to find the common elements in a list of lists, click on the following subheading:
The example finds the common elements in three lists, but you can use this approach with as many lists as necessary.
We used the set()
class to convert the first list to a set
.
The set() class takes an
iterable optional argument and returns a new set
object with elements taken
from the iterable.
list1 = ['a', 'b', 'c'] print(set(list1)) # ๐๏ธ {'a', 'b', 'c'}
intersection()
method.The
intersection()
method returns a new set
with elements common to the provided objects.
list1 = ['a', 'b', 'c'] list2 = ['a', 'z', 'c'] list3 = ['a', 'x', 'c'] common_elements = list( set(list1).intersection(list2, list3) ) print(common_elements) # ๐๏ธ ['a', 'c'] print(len(common_elements)) # ๐๏ธ 2
len()
function if you need to get the number of common elements between the lists.Alternatively, you can use a list comprehension.
This is a three-step process:
list1 = ['a', 'b', 'c'] list2 = ['a', 'z', 'c'] list3 = ['a', 'x', 'c'] common_elements = [ element for element in list1 if element in list2 and element in list3 ] print(common_elements) # ๐๏ธ ['a', 'c']
We used a list comprehension to iterate over the first list.
On each iteration, we check if the current item is present in the two other lists and return the result.
The new list only contains the elements that are present in all three lists.
The in operator tests
for membership. For example, x in l
evaluates to True
if x
is a member of
l
, otherwise, it evaluates to False
.
Alternatively, you can use a simple for loop.
This is a three-step process:
for
loop to iterate over the first list.list1 = ['a', 'b', 'c'] list2 = ['a', 'z', 'c'] list3 = ['a', 'x', 'c'] common_elements = [] for element in list1: if element in list2 and element in list3: common_elements.append(element) print(common_elements) # ๐๏ธ ['a', 'c']
We used a for
loop to iterate over the first list.
On each iteration, we use the in
operator to check if the current item is
contained in the other two lists.
If the condition is met, we append the value to a new list.
The list.append() method adds an item to the end of the list.
my_list = ['bobby', 'hadz'] my_list.append('com') print(my_list) # ๐๏ธ ['bobby', 'hadz', 'com']
The method returns None as it mutates the original list.
To find the common elements in a list of lists:
set
.intersection()
method on the set
.list_of_lists = [ [4, 7, 9], [4, 9, 9], [4, 1, 9] ] common_elements = set( list_of_lists[0] ).intersection(*list_of_lists) print(common_elements) # ๐๏ธ {9, 4} print(list(common_elements)) # ๐๏ธ [9, 4]
We used the set()
class to convert the first nested list to a set
object.
list_of_lists = [ [4, 7, 9], [4, 9, 9], [4, 1, 9] ] print(set(list_of_lists[0])) # ๐๏ธ {9, 4, 7}
intersection()
method.The
intersection()
method returns a new set
with elements common to both set
objects.
list_of_lists = [ [4, 7, 9], [4, 9, 9], [4, 1, 9] ] common_elements = set( list_of_lists[0] ).intersection(*list_of_lists) print(common_elements) # ๐๏ธ {9, 4} print(list(common_elements)) # ๐๏ธ [9, 4]
We called the intersection()
method on the set
and used the iterable
unpacking operator to unpack the list of lists in the call to the method.
The * iterable unpacking operator enables us to unpack an iterable in function calls, in comprehensions and in generator expressions.
You can imagine that the nested lists got passed as multiple, comma-separated
arguments to the intersection
method.
The intersection
method then returned a set
object containing the common
elements in the lists.
Use the list()
class if you need to convert the set
to a list.
The list class takes an iterable and returns a list object.
Alternatively, you can use the intersection_update()
method.
This is a three-step process:
set
.for
loop to iterate over the rest of the list.intersection_update
method to find the common elements.list_of_lists = [ [4, 7, 9], [4, 9, 9], [4, 1, 9] ] common_elements = set(list_of_lists[0]) for l in list_of_lists[1:]: common_elements.intersection_update(l) print(common_elements) # {9, 4}
We converted the first nested list to a set
and stored the result in a
variable.
We then used a for
loop to iterate over the rest of the list.
list_of_lists[1:]
starts at index 1
and goes to the end of the list.On each iteration, we use the intersection_update
method to update the set
with the intersection of itself and the list.
The
intersection_update
method updates the set
in place.
After the last iteration, the variable stores the common elements in the list of lists.
You can learn more about the related topics by checking out the following tutorials: