Extract Common/Non-common/Unique Elements from Multiple Lists in Python
In Python, you can extract common, non-common, and unique elements from multiple lists by converting each list into a set and performing set operations.
To learn more about the set
data type and set operations, refer to this article:
If you want to extract duplicate elements from a single list, refer to the following article:
Keep in mind that, while these examples use lists, you can apply the same process to tuples.
Extract common elements among multiple lists
You can extract common elements in multiple lists using the &
operator on set
. Note that sets do not have an order, so the output order is not guaranteed.
l1 = ['a', 'b', 'c']
l2 = ['b', 'c', 'd']
l3 = ['c', 'd', 'e']
l1_l2_and = set(l1) & set(l2)
print(l1_l2_and)
# {'c', 'b'}
print(type(l1_l2_and))
# <class 'set'>
If you want to convert the set
back to a list
, use list()
. As mentioned above, the order is not preserved when converting to a set
. Therefore, the order may change when converting back to a list, although it could coincidentally remain the same.
l1_l2_and_list = list(l1_l2_and)
print(l1_l2_and_list)
# ['c', 'b']
print(type(l1_l2_and_list))
# <class 'list'>
To get the count of common elements, use len()
.
print(len(l1_l2_and))
# 2
The same applies to three or more lists.
print(set(l1) & set(l2) & set(l3))
# {'c'}
Remove elements common to another list
To remove elements common to another list, use the -
operator on set
. The result may be an empty set
.
l1 = ['a', 'b', 'c']
l2 = ['b', 'c', 'd']
l3 = ['c', 'd', 'e']
print(set(l1) - set(l2))
# {'a'}
print(set(l2) - set(l1))
# {'d'}
print(set(l2) - set(l1) - set(l3))
# set()
As mentioned above, if you want to convert back to a list
or get the number of elements, use list()
and len()
. Examples are omitted. The same applies to the following sections.
Extract elements not common to multiple lists
To get the symmetric difference (the set of elements contained in either of the two sets but not both), also known as the disjunctive union, use the ^
operator.
l1 = ['a', 'b', 'c']
l2 = ['b', 'c', 'd']
l3 = ['c', 'd', 'e']
print(set(l1) ^ set(l2))
# {'d', 'a'}
Be cautious when using the ^
operator with three lists, as it will also include elements common to all lists.
print(set(l1) ^ set(l2) ^ set(l3))
# {'c', 'a', 'e'}
To extract elements contained only in one of the lists, you can create a concatenated list of all lists and extract elements with a count of 1
. Use list comprehension and the count()
method to get the number of elements.
By converting the list
to a set
, you can avoid counting the same element values multiple times.
l_all = l1 + l2 + l3
print(l_all)
# ['a', 'b', 'c', 'b', 'c', 'd', 'c', 'd', 'e']
print(set(l_all))
# {'c', 'd', 'a', 'b', 'e'}
print([x for x in set(l_all) if l_all.count(x) == 1])
# ['a', 'e']
Note that this method will also exclude elements duplicated in the original lists.
l1_dup = ['a', 'a', 'b', 'c']
l_dup_all = l1_dup + l2 + l3
print([x for x in set(l_dup_all) if l_dup_all.count(x) == 1])
# ['e']
By first removing duplicate elements within each list and processing only the unique elements, you can extract elements exclusive to each list.
l_unique_all = list(set(l1_dup)) + list(set(l2)) + list(set(l3))
print(l_unique_all)
# ['c', 'b', 'a', 'c', 'd', 'b', 'c', 'd', 'e']
print([x for x in set(l_unique_all) if l_unique_all.count(x) == 1])
# ['a', 'e']
Extract unique elements from multiple lists
To extract unique elements from multiple lists, convert the concatenated lists to a set
.
l1 = ['a', 'b', 'c']
l2 = ['b', 'c', 'd']
l3 = ['c', 'd', 'e']
print(set(l1 + l2))
# {'c', 'd', 'b', 'a'}
print(set(l1 + l2 + l3))
# {'c', 'd', 'a', 'b', 'e'}
To preserve the order of the original lists, refer to the following article: