When working with Python, there might be cases where you want to find all mutual elements in 2 lists to identify shared values, determine overlapping elements, or measure the similarity between them.
This concise, example-based article will walk you through a couple of different ways to retrieve elements that are present in 2 given lists in Python. We’ll also discuss the performance (time complexity and space complexity) as well as the pros and cons of each approach so you can make an optimal choice for your use case.
Using set intersection
The main idea of this solution is to convert the lists into sets and then use the &
operator to find the intersection of the sets, which is the set of common elements.
Example:
# Define two lists
list1 = ['dog', 'turtle', 'elephant', 'slingacademy.com']
list2 = ['turtle', 'slingacademy.com', 'giraffe', 'lion']
# Convert the lists into sets
set1 = set(list1)
set2 = set(list2)
# Find the intersection of the sets
common_set = set1 & set2
# Convert the set back into a list if needed
common_list = list(common_set)
# Print the result
print(common_list)
Output:
['turtle', 'slingacademy.com']
Performance: This approach has a time complexity of O(n + m)
, where n and m are the lengths of the lists. This is because converting a list into a set takes O(n)
time, and finding the intersection of two sets takes O(min(n, m))
time. The space complexity is O(n + m)
as well, since we need to store two sets and one result set.
Some considerations about this approach:
- Pros: This code is concise, easy to understand, and fast for large lists.
- Cons: Does not preserve the order of the elements. It also requires the elements to be hashable, which means they cannot be mutable objects like lists or dictionaries.
Using list comprehension
This approach can be briefly explained in the 2 steps below:
- Use a list comprehension to iterate over one list (e.g.,
list1
) and check if each element is in the other list (e.g.,list2
) using thein
operator. - The result is a new list that contains only the common elements.
Code example:
# Define two lists
list1 = ['dog', 'turtle', 'elephant', 'slingacademy.com', [1, 2, 3]]
list2 = ['turtle', 'slingacademy.com', 'giraffe', [1, 2, 3], 'lion']
# Use a list comprehension to find common elements
common_list = [x for x in list1 if x in list2]
# Print the result
print(common_list)
Output:
['turtle', 'slingacademy.com', [1, 2, 3]]
Performance: This technique has a time complexity of O(n * m)
, where n and m are the lengths of the lists. This is because, for each element in list1
, we need to scan through list2
to check if it is in it. The space complexity is O(n)
since we need to store a new list that may contain up to n
elements (in the worst case).
Some considerations about this approach:
- Pros: Preserves the order of the elements and works for any type of element, even if they are not hashable. The syntax is also concise (one-line fashion).
- Cons: This solution is less efficient than using sets, especially for large lists, since the time complexity is
O(n * m)
.
Conclusion
We’ve explored 2 different techniques to find all common elements in 2 lists. Each one has its own strengths and weakness. If your lists are simple and don’t have unhashable elements, the first is faster. If your lists contain sublists, dictionaries, or other unhashable objects, the later is the way to go.