Compare Lists in Python
This article explains how to compare lists in Python and briefly discusses tuples at the end.
Order comparison for lists
The result of comparing two lists using <
, <=
, >
, and >=
is determined by the following rules:
Collections that support order comparison are ordered the same as their first unequal elements (for example,
[1,2,x] <= [1,2,y]
has the same value asx <= y
). If a corresponding element does not exist, the shorter collection is ordered first (for example,[1,2] < [1,2,3]
is true). 6. Expressions - Value comparisons — Python 3.11.3 documentation
Elements are compared from the beginning, and the comparison result of the lists is the same as the comparison result of the first non-equal elements.
print([1, 2, 3] < [1, 2, 100])
# True
print([1, 2, 3] < [1, 2, 0])
# False
If there is no corresponding element, the one with fewer elements is considered earlier in order.
print([1, 2] < [1, 2, 3])
# True
If there are non-equal elements, the number of elements is irrelevant. Elements are compared in order from the beginning.
print([100] < [1, 2, 3])
# False
The same rules apply to lists with other types of elements, such as strings.
print(['a', 'b', 'c'] < ['a', 'b', 'x'])
# True
An error occurs if the corresponding element is of a type that cannot be compared.
# print([100] < ['a'])
# TypeError: '<' not supported between instances of 'int' and 'str'
Equality comparison for lists
When comparing lists using ==
, it returns True
if all corresponding elements are equal. Otherwise, it returns False
.
print([1, 2, 3] == [1, 2, 3])
# True
print([1, 2, 3] == [3, 2, 1])
# False
print([1, 2, 3] == [1, 2])
# False
Each element is compared using ==
. If the values are equal even if the types are different, it returns True
. The method to determine if they are exactly the same, including the type, will be described later.
print([1, 2, 3] == [True, 2.0, 3 + 0j])
# True
!=
is the negation of ==
.
print([1, 2, 3] != [1, 2, 3])
# False
print([1, 2, 3] != [3, 2, 1])
# True
print([1, 2, 3] != [1, 2])
# True
print([1, 2, 3] != [True, 2.0, 3 + 0j])
# False
Note that is
determines the identity of the objects. If the two lists point to the same object, it returns True
. Otherwise, it returns False
.
l1 = [1, 2, 3]
l2 = [1, 2, 3]
print(l1 is l2)
# False
l1_assign = l1
print(l1_assign)
# [1, 2, 3]
print(l1 is l1_assign)
# True
Checking the exact match of lists
To check if two lists are exactly matched, define a function as follows:
def lists_match(l1, l2):
if len(l1) != len(l2):
return False
return all(x == y and type(x) == type(y) for x, y in zip(l1, l2))
print(lists_match([1, 2, 3], [1, 2, 3]))
# True
print(lists_match([1, 2, 3], [1, 2]))
# False
print(lists_match([1, 2, 3], [True, 2.0, 3 + 0j]))
# False
In this context, an exact match means that the number of elements in the two lists is the same, and the values and types of the corresponding elements are identical.
Processing as sets (partial match, subset, and superset)
Converting a list to a set (set
) allows set operations.
Partial match
To check if two lists partially match, i.e., if they have at least one common element, use the isdisjoint()
method of set
. It accepts not only sets but also other iterable objects, such as lists.
isdisjoint()
returns True
when there are no common elements. Therefore, its negation (not
) is equivalent to determining a partial match.
l1 = [1, 2, 3]
l2 = [3, 4, 5]
l3 = [5, 6, 7]
print(not set(l1).isdisjoint(l2))
# True
print(not set(l1).isdisjoint(l3))
# False
If you want to extract common or uncommon elements from multiple lists, refer to the following article.
Subset and superset
When all elements of set A are included in set B, A is a subset of B, and B is a superset of A. To check if one is a subset or a superset, use the issubset()
and issuperset()
methods of set
.
You can determine whether all elements of one list are included in another list.
l1 = [1, 2]
l2 = [1, 2, 3]
print(set(l1).issubset(l2))
# True
print(set(l2).issuperset(l2))
# True
Other set operations
By using set
, you can perform additional set operations, such as removing elements from one list that also appear in another list and extracting the differences between them.
l1 = [1, 2, 3, 4, 5]
l2 = [2, 3, 4]
print(list(set(l1) - set(l2)))
# [1, 5]
For more details on set
, refer to the following article.
Comparing tuples
Tuple comparisons are similar to list comparisons.
print((1, 2, 3) < (1, 2, 100))
# True
print((1, 2, 3) == (1, 2, 3))
# True
print(set((1, 2)).issubset((1, 2, 3)))
# True
Comparing lists and tuples using <
, <=
, >
, and >=
will result in an error. You need to align the types using list()
or tuple()
.
l = [1, 2, 3]
t = (1, 2, 3)
# print(l <= t)
# TypeError: '<=' not supported between instances of 'list' and 'tuple'
print(l <= list(t))
# True
Comparing lists and tuples using ==
returns False
. By aligning the types using list()
or tuple()
, you can compare corresponding elements.
print(l == t)
# False
print(l == list(t))
# True