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

image-7 How to Compare Two Lists in Python: Methods and Examples

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 Comment