# 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.

import_contacts

## Related Posts

• 26 December

### Repeat List N Times in Python

Table of ContentsUsing the * operatorUsing the numpy.repeat() functionUsing the list comprehension techniqueUsing the itertools.repeat() functionConclusion This tutorial will demonstrate how to repeat list n times in Python. Using the * operator To repeat list n times in Python, use the * operator. Star operator(*) is used to multiply list by number e.g. lst*3 and […]

• 16 December

### Convert List to Comma Separated String in Python

Table of ContentsUse .join() MethodUse .join() with map() MethodUse .join() with List Comprehension Use .join() Method To convert a list to a comma separated string in Python, use the .join() method. join() method takes all elements in an iterable and joins them into one string with delimiter as separator. [crayon-63b2d91708fa2472492733/] [crayon-63b2d91708fa6173679731/] First, we created a […]

• 27 November

### Remove Punctuation from List in Python

Table of ContentsUse for loop with replace() MethodUse Nested List ComprehensionUse str.translate() Function Use for loop with replace() Method To remove punctuation from list in Python: Use the for loop to iterate over each word (element) of given list. Use a nested loop to iterate over each character in the word. Use the if statement […]

• 04 October

### Get Every Other Element in List in Python

Table of ContentsGet Every Other Element in List in PythonUsing List SlicingUsing the list.append() function along with the range() functionUsing list comprehension and the range() function with conditionalsUsing list comprehension and the range() function with stepUsing the enumerate() function along with list comprehensionUsing a lambda functionConclusion. A list is one of the more versatile data […]

• 23 September

### Add Tuple to List in Python

Table of ContentsAdd tuple to list in PythonWays to Add Tuple to List in Python (adding the Tuple as An Element)Using the insert() Function to Add Tuple to List in PythonUsing the append() Function to Add Tuple to List in PythonWays to Add Tuple to List in Python (adding the Tuple’s Elements to A List)Using […]

• 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.Conclusion In this post, we will […]

## Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.