How to Find Common Values in Multiple Python Lists
Identifying shared elements across multiple lists is a common task in data analysis and programming.
This guide explores efficient methods for finding common values in multiple lists and in lists of lists in Python, using set operations, list comprehensions, and for
loops.
Finding Common Values Using Set Intersection
Sets provide an efficient way to find common elements because set operations are highly optimized for membership testing.
list1 = ['a', 'b', 'c']
list2 = ['a', 'z', 'c']
list3 = ['a', 'x', 'c']
common_elements = list(set(list1).intersection(list2, list3))
print(common_elements) # Output: ['a', 'c']
print(len(common_elements)) #Output: 2
set(list1)
converts the first list to a set. This removes duplicates and allows for efficient intersection operations..intersection(list2, list3)
finds the common elements between the set created fromlist1
andlist2
,list3
. You can provide any number of lists as the arguments.list(...)
converts the resulting set back into a list.
Finding Common Values Using List Comprehensions
List comprehensions offer a more explicit, though potentially less performant, way to find common elements:
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) # Output: ['a', 'c']
- The
[element for element in list1 if ... ]
iterates over the first list. - For each item, the condition
element in list2 and element in list3
verifies the presence of the element in the other two lists, ensuring that the result contains only the values present in all three lists.
Finding Common Values Using a for
Loop
A for
loop provides the most explicit, though least concise, method for identifying common elements:
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) # Output: ['a', 'c']
- The loop iterates through the elements of
list1
- For each element, the code checks if it is present in both
list2
andlist3
using thein
operator. - If it is, the element is added to the
common_elements
list.
Finding Common Elements in a List of Lists
When dealing with a list of lists, you can apply set intersection to find the common elements across all sublists.
Using set intersection
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) # Output: {9, 4}
print(list(common_elements)) # Output: [9, 4]
- The
set(list_of_lists[0])
converts the first element into a set, making it possible to use theintersection()
method. - The
intersection(*list_of_lists)
then finds the common elements of this set, and all of the nested lists. - The asterisk (
*
) unpacks the list of lists, passing each sublist as a separate argument tointersection()
. This is crucial for comparing all lists.
Using intersection_update()
The intersection_update()
method updates a set with the common elements of itself with the elements of another iterable.
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) # Output: {9, 4}
- The
intersection_update
will modify thecommon_elements
set in place.