Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Python Tutorial
  • Interview Questions
  • Python Quiz
  • Python Glossary
  • Python Projects
  • Practice Python
  • Data Science With Python
  • Python Web Dev
  • DSA with Python
  • Python OOPs
Open In App
Next Article:
How to Compare Two Iterators in Python
Next article icon

How to compare two lists in Python?

Last Updated : 12 Dec, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

In Python, there might be a situation where you might need to compare two lists which means checking if the lists are of the same length and if the elements of the lists are equal or not. Let us explore this with a simple example of comparing two lists.

Python
a = [1, 2, 3, 4, 5] b = [1, 2, 3, 4, 5]  # Compare the two lists directly to #Check if they are identical res = a == b print(res) 

Output
True 

Let’s explore other methods to compare two lists in Python.

Table of Content

  • Using collections.Counter()
  • Using sort()
  • Using sorted()
  • Using set()
  • Using all() and zip()

Using collections.Counter()

counter() function of the collections module is used to sort and store the list elements in the form of a dictionary which makes it easier to compare two lists.

Python
import collections    a = [1, 2, 3, 4, 5] b = [5, 4, 3, 2, 1]  # Counter creates a dictionary-like object  res = collections.Counter(a) == collections.Counter(b) print(res) 

Output
True 

Using sort()

Python list sort() function is used to sort a list in either ascending or descending order. It modifies the original list. Once the lists are sorted, they can be compared using the equality operator.

Python
a = [1, 2, 3, 4, 5] b = [5, 4, 3, 2, 1]  # Using sort() to sort both lists in place. res = a.sort() == b.sort() print(res) 

Output
True 

Using sorted()

sorted() function does not modify the original list, instead creates a new list object. Once the lists are sorted, they can be compared using the equality operator.

Python
a = [1, 2, 3, 4, 5] b = [5, 4, 3, 2, 1]  # Use the sorted() to create  # sorted copies of the lists  # Sorts the first list a_sorted = sorted(a)  # Sorts the second list b_sorted = sorted(b)  res = a_sorted == b_sorted print(res) 

Output
True 

Using set()

set() function is used to onvert an object into a set. This method to compare the lists is useful when the elements of the lists are unordered.

Python
a = [1, 2, 3, 4, 5] b = [5, 4, 3, 2, 1]  # Convert the first list to a set a_set = set(a)  # Convert the second list to a set b_set = set(b)  res = a_set == b_set print(res) 

Output
True 

Using all() and zip()

The all() function along with zip() pairs elements from both lists and checks if all corresponding elements are equal. This method works best if the lists are sorted.

Python
a = [1, 2, 3, 4, 5] b = [5, 4, 3, 2, 1]  # Use the all() function along with zip() to  #compare both lists element by element res = all(x == y for x, y in zip(a, b)) print(res) 

Output
False 

Next Article
How to Compare Two Iterators in Python

T

tanyasehr88x
Improve
Article Tags :
  • Python
  • Python Programs
  • python-list
  • Python list-programs
Practice Tags :
  • python
  • python-list

Similar Reads

  • How to Compare Two Iterators in Python
    Python iterators are powerful tools for traversing through sequences of elements efficiently. Sometimes, you may need to compare two iterators to determine their equality or to find their differences. In this article, we will explore different approaches to compare two iterators in Python. Compare T
    3 min read
  • Python List Comprehension With Two Lists
    List comprehension allows us to generate new lists based on existing ones. Sometimes, we need to work with two lists together, performing operations or combining elements from both. Let's explore a few methods to use list comprehension with two lists. Using zip() with List ComprehensionOne of the mo
    3 min read
  • How to Compare Two Dictionaries in Python
    In this article, we will discuss how to compare two dictionaries in Python. The simplest way to compare two dictionaries for equality is by using the == operator. Using == operatorThis operator checks if both dictionaries have the same keys and values. [GFGTABS] Python d1 = {'a': 1, 'b
    2 min read
  • Two For Loops in List Comprehension - Python
    List comprehension is a concise and readable way to create lists in Python. Using two for loops in list comprehension is a great way to handle nested iterations and generate complex lists. Using two for-loopsThis is the simplest and most efficient way to write two for loops in a list comprehension.
    2 min read
  • Comparing Python Lists Without Order
    Sometimes we need to compare two lists without worrying about the order of elements. This is particularly useful when checking if two lists contain the same elements regardless of their arrangement. In this article, we'll explore different ways to compare Python lists without considering order. Usin
    3 min read
  • How to Zip two lists of lists in Python?
    zip() function typically aggregates values from containers. However, there are cases where we need to merge multiple lists of lists. In this article, we will explore various efficient approaches to Zip two lists of lists in Python. List Comprehension provides a concise way to zip two lists of lists
    3 min read
  • Python - How to Check if two lists are reverse equal
    Sometimes, while working with Python lists, we can have a problem in which we need to check if two lists are reverse of each other. This kind of problem can have application in many domains such as day-day programming and school programming. Let's discuss certain ways in which this task can be perfo
    6 min read
  • Compare Two Xml Files in Python
    We are given two XML files and our task is to compare these two XML files and find out if both files are some or not by using different approaches in Python. In this article, we will see how we can compare two XML files in Python. Compare Two XML Files in PythonBelow are the possible approaches to c
    3 min read
  • Compare Two Csv Files Using Python
    We are given two files and our tasks is to compare two CSV files based on their differences in Python. In this article, we will see some generally used methods for comparing two CSV files and print differences. file1.csv contains Name,Age,CityJohn,25,New YorkEmily,30,Los AngelesMichael,40,Chicago fi
    2 min read
  • Remove common elements from two list in Python
    When working with two lists in Python, we may need to remove the common elements between them. A practical example could be clearing out overlapping tasks between two to-do lists. The most efficient way to remove common elements between two lists is by using sets. [GFGTABS] Python a = [1, 2, 3, 4, 5
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences