- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

The list in python is a collection of similar items. We may at times need to compare data items in the two lists to perform certain operations. We will discuss certain methods to compare two lists in python.

The list.sort() method sorts the two lists and the == operator compares the two lists item by item which means they have equal data items at equal positions. This checks if the list contains equal data item values but it does not take into account the order of elements in the list. This means that the list [1,2,3] will be equal to the list [2,1,3] according to this method of comparison.

def compareList(l1,l2): l1.sort() l2.sort() if(l1==l2): return "Equal" else: return "Non equal" l1=[1,2,3] l2=[2,1,3] print("First comparison",compareList(l1,l2)) l3=[1,2,3] l4=[1,2,4] print("Second comparison",compareList(l3,l4))

First comparison Equal Second comparison Non equal

This method tests for the equality of the lists by comparing frequency of each element in first list with the second list. This method also does not take into account the order of the elements of the list.

import collections def compareList(l1,l2): if(collections.Counter(l1)==collections.Counter(l2)): return "Equal" else: return "Non equal" l1=[1,2,3] l2=[2,1,3] print("First comparison",compareList(l1,l2)) l3=[1,2,3] l4=[1,2,4] print("Second comparison",compareList(l3,l4))

First comparison Non equal Second comparison Equal

This method first compares each element of the two lists and store those as summation of 1, which is then compared with the length of the other list. For this method, we have to first check if the lengths of both the lists are equal before performing this computation.

This method also checks for the order of the elements. This means that the list [1,2,3] is not equal to the list [2,1,3].

def compareList(l1,l2): if(len(l1)==len(l2) and len(l1)==sum([1 for i,j in zip(l1,l2) if i==j])): return "Equal" else: return "Non equal" l1=[1,2,3] l2=[2,1,3] print("First comparison",compareList(l1,l2)) l3=[1,2,3] l4=[1,2,3] print("Second comparison",compareList(l3,l4))

First comparison Non equal Second comparison Equal

As in the above example, for the first comparison, the data items in both the lists are equal but the order of the elements is different. Therefore, the first comparison returns not equal.

This is a modification of the first method. In this method, the lists are compared without sorting and thus, this method takes into account the order of the data items in the lists.

def compareList(l1,l2): if(l1==l2): return "Equal" else: return "Non equal" l1=[1,2,3] l2=[2,1,3] print("First comparison",compareList(l1,l2)) l3=[1,2,3] l4=[1,2,3] print("Second comparison",compareList(l3,l4))

First comparison Non equal Second comparison Equal

These are some of the methods to compare lists in python, both taking into account the order of the data items as well as without taking into account the order of data items.

- Related Questions & Answers
- How do we compare two lists in Python?
- How to compare two lists for equality in C#?
- How do we compare the elements of two lists in Python?
- Dividing two lists in Python
- In Python how to create dictionary from two lists?
- Adding two Python lists elements
- How to compare two lists and add the difference to a third list in C#?
- How to compare two strings using regex in Python?
- Merge Two Sorted Lists in Python
- Combining two sorted lists in Python
- How to map two lists into a dictionary in Python?
- How do we compare two tuples in Python?
- How do we compare two dictionaries in Python?
- Python program to compare two Pandas series
- Intersection of Two Linked Lists in Python

Advertisements