How to compare two lists in Python

In this Python tutorial, we will see how to compare two lists in Python. We will see different kinds of methods with illustrative examples regarding this, comparing two lists in Python.

Python lists are used to store multiple items in a single variable. Lists are created using square brackets[], for example:

Countries = ['America', 'Australia', 'Canada', 'Russia', 'India', 'China']

Cricket_team = ['Australia', 'England', 'India', 'West Indies', 'South Africa']

See above, we have two different kinds of lists(Countries, Cricket_team), and they have some common items in them. Python provides multiple methods to compare two lists. We can compare these two lists using different methods.

Comparing two Lists in Python

The various different ways to compare two Lists in Python, they are:

  • Simple Comparison: == Operator
  • Using List Difference: – Operator or difference() method
  • Using set() for Unordered Comparison
  • Using sorted() for Sorted Comparison

We will see them one by one with different examples.

Method-1: Comparing two lists in Python using Simple Comparison: == Operator

Python’s == operator can be used for a direct comparison of two Python lists. It will return True if both lists have the same elements in the same order, otherwise, it returns False.

Let’s consider an example, Suppose we have two Python lists that contain the names of states in the Midwestern region:

midwest1 = ["Illinois", "Indiana", "Iowa", "Kansas", "Michigan", "Minnesota", "Missouri", "Nebraska"]
midwest2 = ["Illinois", "Indiana", "Iowa", "Kansas", "Michigan", "Minnesota", "Missouri", "Nebraska"]

print(midwest1 == midwest2)

The Output is: Here since the order and content of both lists are the same, it returns True.

True
Comparing two lists in Python using == Operator

This way we can use a simple comparison: == operator to compare two lists in Python.

Method-2: Comparing two lists in Python using List Difference: – Operator or difference() method

We can also find the difference between two lists in Python by converting them into sets and using the – operator or the difference() method in Python. They return a set that contains the difference between two sets, meaning the elements present in the first set but not in the second. If both Python lists are identical, the difference will be an empty set.

For Example: Here we will have two different lists in Python containing names of some states in the USA. Firstly, we will convert both lists in a Python set using set(), then we will use the – operator or the difference() method and then compare the result to an empty Python set.

If the result prints True then we have identical Python lists and if the result prints False then the Python lists are not identical.

# using - operator
list1 = ["California", "Texas", "New York", "Florida", "Illinois"]
list2 = ["California", "Texas", "New York", "Florida", "Illinois"]

print(set(list1) - set(list2) == set())

The output of this block of code is:

True
Comparing two lists in Python using - Operator
# using difference() method
list1 = ["California", "Texas", "New York", "Florida", "Illinois"]
list2 = ["California", "Texas", "New York", "Florida", "Illinois"]

print(set(list1).difference(set(list2)) == set())

The output for this block of code is:

True
Comparing two lists in Python using difference() method

They both give the same output as True, which means we have two identical Python lists with us.

Note: Python can have duplicate values while Python sets don’t. When we convert the list with duplicate values into a Python set, the duplicate value gets eliminated. In that case, both methods can show the wrong answer.

Method-3: Comparing two lists in Python using set() for Unordered Comparison

The set() function is used when we want to check whether two Python lists have the same elements, irrespective of their order. This function converts a Python list into a set, which is an unordered collection of unique elements.

For example, let’s say we have two Python lists containing the names of the west coast states. The first list is the visited states, and the second list is all west coast states:

visited_states = ["California", "Nevada"]
west_coast_states = ["California", "Oregon", "Washington", "Nevada"]


print(set(visited_states) == set(west_coast_states))

The output is:

False

As Both Python lists do not have all the same elements, so get the result as False.

Comparing two lists in Python using set() for Unordered Comparison

This we can use set() for the unordered comparison of two Python lists.

Note: Python can have duplicate values while Python set don’t. When we convert the list with duplicate values into a Python set, the duplicate value get eliminated. In that case, both methods can show the wrong answer.

Method-4: Comparing two lists in Python using sorted() for Sorted Comparison

Another way to compare two Python lists, disregarding the order of elements, is by sorting the lists first using the sorted() method.

For example, let’s say we have two Python lists containing the top 5 U.S companies but in a different order:

original_order = ["Apple", "Microsoft", "Amazon", "Google", "Facebook"]

different_order = ["Amazon", "Facebook", "Apple", "Microsoft", "Google"]

print(sorted(original_order) == sorted(different_order))

Even though the original_order and different_order lists are not identical (the companies are in a different order), when we sort the Python lists before comparing them, the order of the companies no longer matters.

The Output is: The comparison sorted(original_order) == sorted(different_order) returns True because both sorted Python lists contain the same companies.

True
Comparing two lists in Python using sorted() for sorted comparison

This way we can use sorted() for the sorted comparison of two Python lists.

Conclusion:

In conclusion, we saw a comparison of two Python lists can be done in numerous ways depending on what we want to achieve: we may want to know if two lists are equal, or perhaps we want to find the differences between the lists or anything else.

We can easily do this using all the above-mentioned methods like using the == operator, – operator or difference() method, set() method, or sorted() method.

You may also like to read: