How to compare lists in Python

In this post, we will see how to compare lists in Python.

Python compare lists

Lists in Python are mutable and can store a finite number of elements. We cannot determine whether a list is greater or less than any other list based on general criteria. So when we talk about comparing lists, we mean checking whether two lists have the same elements or not.

How to compare lists in Python

In this article, several ways are discussed to compare two lists in Python. Two lists can only be equal if they are of the same length so in this article all examples assume that the lists being compared are of the same length.

Using the sort() function and == operator

We can use the equality operator (==) to compare two lists and check whether they are equal or not. It is advisable to use the sort() function before comparing because sometimes the lists may contain the same elements but they might be shuffled. The sort() function will sort them. Also, this function sorts the original list and does not create a new list.

See the code below.

Output:

True
False

In the above example,

  • We created three lists a, b, and c.
  • We sorted the lists and compared them.
  • Lists a and b were equal and True is returned.
  • Lists a and c were not equal so we get False.
  • The sort() function can be used with any of the methods discussed below.

Using the map() and reduce() functions

We can implement a simple lambda function to compare two lists. Such functions are used to implement simple, one-line logic.

For this, we will use the map() and reduce() functions. The map() function takes an iterable and applies a function to every element of this object. The reduce() function will be applied to the map object to compare the two elements.

It is necessary to sort the lists before using these functions.

We implement the above logic in the following code snippet.

Output:

Equal

Using the set() function and == operator

The set() function returns a set object that is unordered and contains distinct elements. We can compare these sets using the == operator.

There is no requirement to sort these lists a set is unordered.

For example,

Output:

True
False

In the above code,

  • We first created three lists.
  • These lists were converted to sets using the set() function.
  • These sets were compared for equality using the == operator.

Using the for loop

We can use the for loop to iterate over both lists and compare each element individually. If even a single pair of elements do not match, we break out of the loop and know that the lists are not equal.

See the code below.

Output:

Equal

Using the set() function and - operator

We can convert two lists to a set using the set() function and calculate their difference. The difference between two the sets is the total elements present in the first set and not in the second set. We can use this difference to compare the two lists. If the difference is an empty set, they are equal, else they are not.

See the following example.

Output:

Equal

Using the collections.Counter() class and == operator

The Counter is a sub-class from the collections module that stores elements in a hashable object with a dictionary-like structure. We can store two lists in such objects and compare them using the == operator.

No requirement to sort the lists beforehand.

For example,

Output:

True

Using the zip(), sum(), and len() functions

The zip() function returns a zip-type object by combining elements of two iterables. The len() function calculates the length of the iterable.

We can use these functions with the sum() function to calculate the total number of indexes where the elements are different. If this sum is 0, then the lists are equal, else they are not equal. It is essential to sort the lists before using these functions.

We implement this logic in the following code snippet.

Output:

Equal

Using the cmp() function

The cmp() function returns 1,-1, and 0 based on the comparison between two integers. We can use it with lists, and if the result is 0 then the two lists are equal.

This function works only in Python 2.

For example,

Output:

Equal

Conclusion

In this article, we discussed different ways to compare two lists in Python. The == operator is used in many methods, and we discovered some innovative ways to achieve the same using sets, cmp(), and zip() methods.

Was this post helpful?


import_contacts

You may also like:

Related Posts

  • 24 May

    Replace Comma with Space in List in Python

    Table of ContentsHow to replace comma with space in list in PythonUsing the replace() function to replace comma with space in list in PythonUsing the re.sub() function to replace comma with space in list in PythonUsing the split() and join() functions to replace comma with space in list in Python.ConclusionWas this post helpful? In this […]

  • 19 May

    Compare list elements with each other in Python

    Table of ContentsCompare all elements in list PythonWays to compare list elements with each other in PythonUsing the for loop to compare all elements in list PythonUsing the itertools.combinations() function to compare all elements in list PythonConclusionWas this post helpful? In this post, we will see how to compare list elements with each other in […]

  • 18 May

    How to Deep Copy a List in Python

    Table of ContentsWhat is a list in Python?What are shallow copy and deep copy operations in Python?Shallow CopyDeep CopyHow to deep copy a list in Python?Using the deepcopy() function from the copy module to deep copy a list in Python.Using a user-defined function to deep copy a list in Python.Using list comprehension to deep copy […]

  • 18 May

    Convert Generator to List in Python

    Table of ContentsLists and Generators in PythonWays to convert generator to list in PythonUsing the list() function to convert generator to list in PythonUsing the unpack operator * to convert generator to list in PythonUsing the list comprehension to convert generator to list in PythonUsing the append() function to convert generator to list in PythonConclusionWas […]

  • Create a list from 1 to 100 in Python
    03 May

    Create a List from 1 to 100 in Python

    Table of ContentsWays to create a list from 1 to 100 in PythonUsing the range() function to create a list from 1 to 100 in PythonUsing the numpy.arange() function to create a list from 1 to 100 in PythonUsing the for loop with range() to create a list from 1 to 100 in PythonConclusionWas this […]

  • Python List of dictionaries
    09 December

    List of Dictionaries in Python

    Table of ContentsHow to Create a List of Dictionaries in Python?How to Access Elements of a List of Dictionaries?Search in a List of Dictionaries in PythonUpdate Values in a List of Dictionaries in PythonHow to Sort List of Dictionaries in Python?ConclusionWas this post helpful? We use both lists and dictionaries in our python programs. Lists […]

Leave a Reply

Your email address will not be published.

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.