Register for 45 Day Coding Challenge by CodeStudio And Win Some Exciting Prizes

# How to Compare Two Lists in Python

How to Compare Two Lists in Python | Python allows comparing two list elements by providing various built-in methods. The comparison checks whether the list contains the same elements or not irrespective of the index. Also See:- How to Find the Length of a List in Python

We will see these below Python program examples:–

1. using sort() function and == operator
2. using collection.counter() function
3. using cmp() function
4. using set() function and == operator
5. using reduce() and map() function
6. Compare two lists in python using for loop
7. Python compare two lists element-wise
8. Python compare two lists of strings

## How to Compare Two Lists in Python

How to compare 2 lists in python by using the list.sort() and == operator.

Step1: we define a class called compare which takes 2 lists as parameters list1 and list2.
Step2: Then we use the list.sort() method to both the list and compare them.
Step3: in for loop we use the ‘==’ operator to compare the list

``````def compare(list1,list2):
list1.sort()
list2.sort()
if(list1==list2):
return "Equal"
else:
return "Non equal"
l1=[1,2,3]
l2=[2,1,3]
print("Comparision using 'list.sort()' comparison:-",compare(l1,l2))
l3=[1,2,3]
l4=[1,3,2]
print("Comparison using '==' operator:-",compare(l3,l4))
``````

Output:

Comparision using ‘list.sort()’ comparison:- Equal
comparison using ‘==’ operator:- Equal

How to compare two lists by using collections.counter() method.

The counter() is a method available in the collections framework which contains the count of each element. This also compares the elements irrespective of their orders.

``````import collections
def compare(list1,list2):
if(collections.Counter(list1)==collections.Counter(list2)):
return "Equal"
else:
return "Not equal"

list1=[1,2,3]
list2=[2,1,3]
print("Comparing List1 and list2",compare(list1,list2))

list3=[1,2,3]
list4=[1,2,4]
print("Comparing list3 and list4",compare(list3,list4))
``````

Output:

Comparing list1 and list2 Equal
Comparing list3 and list4 Not equal

How to compare two lists in python using cmp() function.

The cmp() takes two arguments list1 and list2 and returns 1 if the first list is greater and returns -1 if the second list is greater or else returns 0 if both are equal.

Note:- The cmp() method in Python 2.x compares two integers and returns -1, 0, 1 according to comparison. The cmp() does not work in python 3.x.

How to compare two lists using set() function ‘==’ operator.

Here, we will convert the given lists into a set and then compare two lists with the help of the ‘==’ operator.

``````list1 = [1, 2, 3]
list2 = [1, 2, 3]

a = set(list1)
b = set(list2)

if a == b:
print("The list1 and list2 are equal")
else:
print("The list1 and list2 are not equal")
``````

Output:

The list1 and list2 are equal

How to compare two lists by using reduce() and map() functions.

The map() function accepts the function and iterable object as an argument and returns a map object, and reduce() function implements the given function to the iterable object recursively.

``````import functools

list1 = [1,2,3]
list2 = [1,5,4,0,6,7]
list3 = [1,2,3]

if functools.reduce(lambda x, y: x and y,
map(lambda a, b: a == b, list1, list2), True):
print("The list1 and list2 are the same")
else:
print("The list1 and list2 are not the 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 the same")
else:
print("The list1 and list3 are not the same")``````

Output:

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

## Compare Two Lists in Python using For Loop

By using nested for loop we will compare two lists and return the elements that are the same in both the lists.

``````list1 = [1, 2, 3, 4, 5]
list2 = [1, 2, 3, 7, 8]

list = []

for item in list1:
for item1 in list2:
if item == item1:
list.append(item)

print(list)``````

Output:

[1, 2, 3]

## Python Compare Two Lists Element Wise

Now we will compare two lists element-wise and return the element which is present in both the lists by using a set

``````a = [1, 2, 3, 4, 5]
b = [9, 8, 7, 6, 5]

res = set(a) & set(b)

print(list(res))``````

Output:



## Python Compare Two Lists of Strings

Now we will compare two lists of strings by using the collections framework. In collections, we use the counter() method to compare the list of strings.

``````import collections

list1 = ['Know', 'program']
list2 = ['Know', 'program']

if collections.Counter(list1) == collections.Counter(list2):
print("Both List are same")
else:
print("Not the same")
``````

Output:

Both List are same

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!