
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Check if two lists are identical in Python
In python data analysis, we may come across situation when we need to compare two lists and find out if they are identical meaning having same elements or not.
Exmple
listA = ['Mon','Tue','Wed','Thu'] listB = ['Mon','Wed','Tue','Thu'] # Given lists print("Given listA: ",listA) print("Given listB: ",listB) # Sort the lists listA.sort() listB.sort() # Check for equality if listA == listB: print("Lists are identical") else: print("Lists are not identical")
Output
Running the above code gives us the following result −
Given listA: ['Mon', 'Tue', 'Wed', 'Thu'] Given listB: ['Mon', 'Wed', 'Tue', 'Thu'] Lists are identical
With Counter
The Counter function from collections module can help us in finding the number of occurrences of each item in the list. In the below example we also take two duplicate elements. If the frequency of each element is equal in both the lists, we consider the lists to be identical.
Example
import collections listA = ['Mon','Tue','Wed','Tue'] listB = ['Mon','Wed','Tue','Tue'] # Given lists print("Given listA: ",listA) print("Given listB: ",listB) # Check for equality if collections.Counter(listA) == collections.Counter(listB): print("Lists are identical") else: print("Lists are not identical") # Checking again listB = ['Mon','Wed','Wed','Tue'] print("Given listB: ",listB) # Check for equality if collections.Counter(listA) == collections.Counter(listB): print("Lists are identical") else: print("Lists are not identical")
Output
Running the above code gives us the following result −
Given listA: ['Mon', 'Tue', 'Wed', 'Tue'] Given listB: ['Mon', 'Wed', 'Tue', 'Tue'] Lists are identical Given listB: ['Mon', 'Wed', 'Wed', 'Tue'] Lists are not identical
- Related Articles
- Python program to check whether two lists are circularly identical
- Check if two list of tuples are identical in Python
- Python Program to check if two given matrices are identical
- Check if tuple and list are identical in Python
- C# program to check if two matrices are identical
- Python - Check if all elements in a list are identical
- Program to check if two given matrices are identical in C++
- Java program to check if two given matrices are identical
- Python - Check if two lists have any element in common
- Python program to check if two lists have at least one common element
- Check if element exists in list of lists in Python
- Python - Check if two strings are isomorphic in nature
- Write Code to Determine if Two Trees are Identical in C++
- Python Pandas – Check if two Dataframes are exactly same
- Check if a list exists in given list of lists in Python

Advertisements