Skip to content
  • SiteMap
  • Our Services
  • Frequently Asked Questions (FAQ)
  • Support
  • About Us

UpdateGadh

Update Your Skills.

  • Home
  • Projects
    •  Blockchain projects
    • Python Project
    • Data Science
    •  Ai projects
    • Machine Learning
    • PHP Project
    • React Projects
    • Java Project
    • SpringBoot
    • JSP Projects
    • Java Script Projects
    • Code Snippet
    • Free Projects
  • Tutorials
    • Ai
    • Machine Learning
    • Advance Python
    • Advance SQL
    • DBMS Tutorial
    • Data Analyst
    • Deep Learning Tutorial
    • Data Science
    • Nodejs Tutorial
  • Blog
  • Contact us
  • Toggle search form
How to Compare Two Lists in Python: Methods and Examples - How to Compare Two Lists in Python

How to Compare Two Lists in Python: Methods and Examples

Posted on January 6, 2025January 8, 2025 By Rishabh saini No Comments on How to Compare Two Lists in Python: Methods and Examples

Compare Two Lists in Python

In Python, comparing two lists involves checking whether their elements are the same or not. Python offers several ways to accomplish this, catering to different requirements, such as checking order sensitivity or handling unordered data.

Complete Python Course with Advance topics:-Click here

Compare Two Lists in Python

Sample Lists for Comparison

list1 = [11, 12, 13, 14, 15]
list2 = [11, 12, 13, 14, 15]

Output:
The lists are equal.

Methods to Compare Two Lists in Python

  1. Using the == Operator
  2. Using the set() Function
  3. Using the sort() Method
  4. Using the collections.Counter() Function
  5. Using map() and functools.reduce()

1. Using the == Operator

The simplest way to compare two lists is by using the == operator. This checks for both the values and their order.

Example:

list1 = [11, 12, 13, 14, 15]
list2 = [11, 12, 13, 14, 15]

if list1 == list2:
    print("The lists are equal")
else:
    print("The lists are not equal")

Output:

The lists are equal

Explanation:
The == operator compares both the order and elements of the lists.

2. Using the set() Function

The set() function converts lists to sets, ignoring the order of elements. The comparison is then performed using the == operator.

Example:

list1 = [11, 12, 13, 14, 15]
list2 = [15, 14, 13, 12, 11]

if set(list1) == set(list2):
    print("The lists are equal")
else:
    print("The lists are not equal")

Output:

The lists are equal

Explanation:
The set() function ignores the order, ensuring equality is based solely on the elements.

3. Using the sort() Method

Sorting the lists before comparison ensures that the order does not affect the equality check.

Example:

list1 = [10, 20, 30, 40, 50]
list2 = [50, 40, 30, 20, 10]

list1.sort()
list2.sort()

if list1 == list2:
    print("The lists are equal")
else:
    print("The lists are not equal")

Output:

The lists are equal

Explanation:
The sort() method rearranges elements in ascending order, making the comparison straightforward.

4. Using collections.Counter()

The collections.Counter class counts the frequency of elements in each list and compares them as dictionaries.

Example:

from collections import Counter

list1 = [10, 20, 30, 40, 50]
list2 = [50, 40, 30, 20, 10]

if Counter(list1) == Counter(list2):
    print("The lists are equal")
else:
    print("The lists are not equal")

Output:

The lists are equal

Explanation:
Counter creates a dictionary with elements as keys and their counts as values, making it order-insensitive.

5. Using map() and functools.reduce()

This method involves using map() to compare elements pairwise and reduce() to aggregate the results.

Example:

from functools import reduce

list1 = [10, 20, 30, 40, 50]
list2 = [10, 20, 30, 40, 50]
list3 = [50, 40, 30, 20, 10]

# Compare list1 and list2
result1 = reduce(lambda x, y: x and y, map(lambda a, b: a == b, list1, list2), True)

# Compare list1 and list3
result2 = reduce(lambda x, y: x and y, map(lambda a, b: a == b, list1, list3), True)

if result1:
    print("list1 and list2 are equal")
else:
    print("list1 and list2 are not equal")

if result2:
    print("list1 and list3 are equal")
else:
    print("list1 and list3 are not equal")

Output:

list1 and list2 are equal
list1 and list3 are not equal

Explanation:
map() compares corresponding elements, and reduce() aggregates the results. Order matters in this method.

Key Considerations

  • Use set() or collections.Counter() when order doesn’t matter.
  • Use == or sort() when order is significant.
  • Use map() and reduce() for custom comparisons or when additional processing is required.

Download New Real Time Projects :-Click here
PHP PROJECT:- CLICK HERE


How to Compare Two Lists in Python: Methods and Examples
how to compare two lists in python
python compare two lists return non matches
python compare two lists element wise
compare two lists in python using for loop
compare two lists in python
how to compare two lists in python
python compare two lists for differences
python compare two lists of strings
compare two lists in python and return indices of matched values
list comparison in python w3schools
compare two lists online
set in python
how to compare two lists in python

Post Views: 933
Python Interview Question Tags:compare two lists in python, compare two lists in python using for loop, how to compare two lists in python, list in python, Python, python compare, python compare dates, python compare dictionaries, python compare sets, python compare strings, python compare strings ignore case, python compare two dictionaries, python compare two files, python compare two lists, python compare two strings, python lists, python lists vs arrays

Post navigation

Previous Post: Library Management System Using Python Django
Next Post: SQL USE Database Statement

More Related Articles

Bubble Sort in Python Bubble Sort in Python Python Interview Question
How to Read a Text File in Python How to Read a Text File in Python Python Interview Question
Declare a Variable in Python How to Declare a Variable in Python Python Interview Question

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

You may also like

  1. How to Install Python on Windows: A Step-by-Step Guide
  2. How to Run Python Program: A Comprehensive Guide for Python Programmers
  3. How to Create a Dictionary in Python
  4. How to Install Matplotlib in Python
  5. How to Create a DataFrame in Python
  6. Insertion Sort in Python

Most Viewed Posts

  1. Top Large Language Models in 2025
  2. Online Shopping System using PHP, MySQL with Free Source Code
  3. login form in php and mysql , Step-by-Step with Free Source Code
  4. Flipkart Clone using PHP And MYSQL Free Source Code
  5. News Portal Project in PHP and MySql Free Source Code
  6. User Login & Registration System Using PHP and MySQL Free Code
  7. Top 10 Final Year Project Ideas in Python
  8. Online Bike Rental Management System Using PHP and MySQL
  9. E learning Website in php with Free source code
  10. E-Commerce Website Project in Java Servlets (JSP)
  • AI
  • ASP.NET
  • Blockchain
  • ChatCPT
  • code Snippets
  • Collage Projects
  • Data Science Project
  • Data Science Tutorial
  • DBMS Tutorial
  • Deep Learning Tutorial
  • Final Year Projects
  • Free Projects
  • How to
  • html
  • Interview Question
  • Java Notes
  • Java Project
  • Java Script Notes
  • JAVASCRIPT
  • Javascript Project
  • JSP JAVA(J2EE)
  • Machine Learning Project
  • Machine Learning Tutorial
  • MySQL Tutorial
  • Node.js Tutorial
  • PHP Project
  • Portfolio
  • Python
  • Python Interview Question
  • Python Projects
  • PythonFreeProject
  • React Free Project
  • React Projects
  • Spring boot
  • SQL Tutorial
  • TOP 10
  • Uncategorized
  • Online Examination System in PHP with Source Code
  • AI Chatbot for College and Hospital
  • Job Portal Web Application in PHP MySQL
  • Online Tutorial Portal Site in PHP MySQL — Full Project with Source Code
  • Online Job Portal System in JSP Servlet MySQL

Most Viewed Posts

  • Top Large Language Models in 2025 (8,612)
  • Online Shopping System using PHP, MySQL with Free Source Code (5,211)
  • login form in php and mysql , Step-by-Step with Free Source Code (4,866)

Copyright © 2026 UpdateGadh.

Powered by PressBook Green WordPress theme