There are a few different ways in which we may want to compare lists, depending on what is important in the comparison.
Identity Matters
In this situation we really want to know whether two lists objects are the same object or not.For this type of comparison, we want to use the is operator.
For example, if we have these three lists:
list_1 = [1, 2, 3]
list_2 = [1, 2, 3]
list_3 = list_1
Then list_1 and list_2 are not the same objects, but list_1 and list_3 are, so we have:
list_1 is list_2 --> False
list_1 is list_3 --> True
Order and Duplicates Matter
In this case we don't care about whether the two lists objects are the same object, we just want to know if they have the same content, and in particular order matters as well as duplicate objects.
For example these two lists would not be considered equal because the items are not ordered in the same way:
[1, 2, 3, 4]
[4, 3, 2, 1]
In the same way, these two lists are not considered equal because they do not contain the same repeated elements:
[1, 1, 2, 2]
[1, 2]
For these types of comparisons, we can just use the == operator.
So we would have:
[1, 2, 3, 4] == [1, 2, 3, 4] --> True
[1, 2, 3, 4] == [4, 3, 2, 1] --> False
Order Ignored, Duplicates Matter
This one is less usual. Here we really want to know if the two lists contain the same elements, including repeated elements, but we don't care about the order of these elements.
For example each of these lists would be considered equal:
[1, 2, 3, 4, 4]
[1, 4, 3, 2, 4]
[4, 4, 3, 2, 1]
So what we really want to know here is whether the number of each distinct element matches between the two lists, irrespective of their order in the list.
The easiest way to do this is to use the Counter object in the collections module.
A Counter object is essentially a multi-set - you can think of it as a dictionary with a key for each unique element in the list and a corresponding value of the number of times the element appears in the list.
So in the above example, the Counter for each list would be the same:
{1: 1, 2: 1, 3:1, 4:2}
We can implement this type of equality testing as follows:
from collections import Counter
list_1 = [1, 2, 3, 4, 4]
list_2 = [1, 4, 3, 2, 4]
Counter(list_1) == Counter(list_2) --> True
Order Ignored, Duplicates Ignored
In this case duplicates can be ignored, and order does not matter. This is basically how sets are compared. Sets do not have duplicate elements, and sets have no ordering.
For example, the following lists would be considered equal:
[1, 2, 2, 3, 3, 4]
[4, 3, 2, 1]
We can easily do this using Python sets, by converting the lists to sets (which will result in duplicate elements being ignored), and comparing the two resulting sets using == (where order is ignored since sets do not have ordering):
list_1 = [1, 2, 2, 3, 3, 4]
list_2 = [4, 3, 2, 1]
set(list_1) == set(list_2) --> True
And there you go, four different ways of comparing lists! Of course, the same works with (finite) iterables in general, not just lists.
The second part, using a dictionary to compare lists! Very interesting. I was thinking you were going to sort first and then compare. Like:
ReplyDeletesorted(list_1) == sorted(list_2)