How do we compare the elements of two lists in Python?



In this article we will look at different ways to compare the elements of two lists in Python. To compare two lists, we compare the data items of one list over the other list and check if they are same or not.

Below are the two lists which are similar.

List_1 = [1,2,3,4,5,6,7]
List_2 = [1,2,3,4,5,6,7]

The output of the above two should be that the lists are same.

Using set() method with == operator

In this method we use the set() method and == operator to compare the elements of the two lists. The python set() method manipulates a list into a set without regard for the order of the items. We also use the equal to operator (==) to compare the list's data items.

Example

In the following example, the two lists will be compared. We turned the lists into a set and used the == operator to compare each element. If all the elements in both lists are equal, the block is executed, and the result is displayed.

list_1 = [1, 2, 3, 4, 5] list_2 = [2, 3, 1, 5, 4] a = set(list_1) b = set(list_2) if a == b: print("The list_1 and list_2 are equal") else: print("The list_1 and list_2 are not equal")

Output

The following output is obtained on executing the above program.

The list_1 and list_2 are equal

Using sort() method with == operator

In this method we use the sort() method and == operator to compare the elements of the two lists. The sort() method which is provided by python is used to sort the elements of the list in ascending or descending order. After sorting the list, we compare the lists using the == operator. The same elements are at the same index position for both the lists which means that the lists are equal.

Example

In the following example, we first sort the elements of the list using the sort() method and then, compare the lists using the == operator.

list_1 = [1, 2, 3, 4, 5] list_2 = [2, 3, 1, 5, 4] list_3 = [1, 5, 6, 3, 4] list_1.sort() list_2.sort() list_3.sort() if list_1 == list_2: print("The list_1 and list_2 are the same") else: print("The list_1 and list_2 are not the same") if list_1 == list_3: print("The list_1 and list_3 are the same") else: print("The list_1 and list_3 are not the same")

Output

The above code produces the following output.

The list_1 and list_2 are the same
The list_1 and list_3 are not the same

Using map() and reduce() methods

We need to import functools to use map() and reduce() methods.

The map() function takes two arguments: a function and a python iterable object (list, tuple, string, etc.) and returns a map object. The function is applied to each list element and returns an iterator in return.

Furthermore, the reduce() method recursively applies the specified function to the iterable object.

We'll combine both strategies in this case. The map() method would apply the function (which may be a user defined or lambda function) to each iterable object, and the reduce() function would handle the recursive application.

Example

The following is an example code to compare two lists using map() and reduce() methods.

import functools list1 = [1, 2, 3, 4, 5] list2 = [1, 5, 6, 3, 4] list3 = [2, 3, 1, 5, 4] if functools.reduce(lambda x, y: x and y, map(lambda a, b: a == b, list1, list2), True): print("The list1 and list2 are same") else: print("The list1 and list2 are not same") if functools.reduce(lambda x, y: x and y, map(lambda a, b: a == b, list1, list3), True): print("The list1 and list3 are same") else: print("The list1 and list3 are the same")

Output

The output of the above code is as follows.

The list1 and list2 are not same
The list1 and list3 are the same

Advertisements