How to Compare Two Lists in Python with Examples

In this article, we will understand the different ways to compare two lists in Python. We often come across situations wherein we need to compare the values of the data items stored in any structure say list, tuple, string, etc.

Comparison is the method of checking the data items of a list against equality with the data items of another list.


Methods to Compare Two Lists in Python

We can use either of the following methods to perform our comparison:

  • The reduce() and map() function
  • The collection.counter() function
  • Python sort() function along with == operator
  • Python set() function along with == operator
  • The difference() function

1. Python reduce() and map() functions

We can use the Python map() function along with functools.reduce() function to compare the data items of two lists.

The map() method accepts a function and an iterable such as list, tuple, string, etc. as arguments.

It applies the passed function to each item of the iterable and then returns a map object i.e. an iterator as the result.

The functools.reduce() method applies the passed function to every element of the input iterable in a recursive manner.

Initially, it would apply the function on the first and the second element and returns the result. The same process will continue on each of the elements until the list has no elements left.

As a combination, the map() function would apply the input function to every element and the reduce() function will make sure that it applies the function in a consecutive manner.

Example:

Output:


2. Python collection.counter() method

The collection.counter() method can be used to compare lists efficiently. The counter() function counts the frequency of the items in a list and stores the data as a dictionary in the format <value>:<frequency>.

If two lists have the exact same dictionary output, we can infer that the lists are the same.

Note: The list order has no effect on the counter() method.

Example:

Output:


3. Python sort() method and == operator to compare lists

We can club the Python sort() method with the == operator to compare two lists.

Python sort() method is used to sort the input lists with a purpose that if the two input lists are equal, then the elements would reside at the same index positions.

Note: The order of the list does not affect this method because we’ll be sorting the lists before comparison.

Further, the == operator is used to compare the list, element by element.

Example:

Output:


4. Python set() method and == operator to compare two lists

Python set() method manipulates the data items of an iterable to a sorted sequence set of data items without taking the order of elements into consideration.

Further, the == operator is used for comparison of the data items of the list in an element-wise fashion.

Example:

Output:


5. Python Custom List Comprehension to Compare Two Lists

We can use Python List comprehension to compare two lists.

Example:

In the above code, we set a pointer element ‘x’ to the list l1 and l3. Further, we check if the element pointed by the pointer element is present in the lists.

Output:


Conclusion

Thus, in this article, we have covered and understood a number of ways to compare multiple lists in Python.

By admin

Leave a Reply

%d bloggers like this: