×

How to Compare two Lists in Python?

How to Compare two Lists in Python

The list is a data structure in Python that can hold values of different data types. The values are enclosed in square brackets [ ]. Lists are mutable which means their values can be changed. An example of a list is-

list1 = [23,’Apple’,3.5,’Mangoes’]

The values present in a list can be accessed through indexing. For instance, list1[1] will return the value ‘Apple’.

The difference between lists and arrays is that lists can hold values of multiple data types whereas arrays hold values of a similar data type.

In this article, we will discuss how to compare two lists in python using the following methods-

  1. Sort()
  2. Collections counter
  3. Using equals operator.

In all the programs, we are using the approach of defining the functions, since they reduce the code size and help us to use the desired functionality for different input parameters.

In each program, the definition of the function contains sort(), collections counter(), and use of equals operator, and then we have passed two lists at a time as an input parameter.

Let us have a look at the first method where we will compare the elements of a list using sort().

In sort() method, we will take lists as input parameters and then apply the sort() method which will arrange the elements in ascending order and then compare its elements

The following program illustrates the same-

 #creating a function
 def comp_list(l1,l2):
     l1.sort()    #using sort()
     l2.sort()
     if(l1==l2):
         print("Both lists are equal")
     else:
         print("Both lists are not equal")
 l1=[1,2,3,4,5]
 l2=[2,4,5,1,3]
 comp_list(l1, l2)  #calling the function
 l3=[11,12,13,14,15]
 l4=[15,11,13,12,14]
 comp_list(l3, l4)  #calling the function
 l5=[20,21,22,23,24]
 l6=[27,28,29,30,31]
 comp_list(l5,l6)   #calling the function 

INPUT-

How to Compare two Lists in Python?

OUTPUT-

How to Compare two Lists in Python?

We can observe the following things-

  1. First, the two lists are passed l1 and l2 in the function comp_list(), and then the results are checked on the execution of our code.
  2. Similarly, we specified the elements in the lists l3,l4,l5, and l6 and then passed l3 and l4 in the function and then l5 and l6.
  3. In the output, we can observe that the elements of l1 and l2, l3 and l4 are equal whereas elements of l5 and l6 are not equal.

In the second method, we will implement it using collections.counter()

The counter() method returns the count of objects(iterables) present in the list. To use it for comparison we will import collections and then use this method.

The following program illustrates the same-

 import collections
 #creating a function
 def comp_list(l1,l2):
     if(collections.Counter(l1)==collections.Counter(l2)):
         print("Both lists are equal")
     else:
         print("Both lists are not equal")
 l1=[1,2,3,4,5]
 l2=[2,4,5,1,3]
 comp_list(l1, l2)  #calling the function
 l3=[11,12,13,14,15]
 l4=[15,11,13,12,14]
 comp_list(l3, l4)  #calling the function
 l5=[20,21,22,23,24]
 l6=[27,28,29,30,31]
 comp_list(l5,l6)   #calling the function 

INPUT-

How to Compare two Lists in Python?

OUTPUT-

How to Compare two Lists in Python?

We can observe the following things-

  1. First, the two lists are passed l1 and l2 in the function comp_list(), and then the results are checked on the execution of our code.
  2. Similarly, we specified the elements in the lists l3,l4,l5, and l6 and then passed l3 and l4 in the function and then l5 and l6.
  3. In the output, we can observe that the elements of l1 and l2, l3 and l4 are equal whereas elements of l5 and l6 are not equal.

The last method which we will discuss is the comparison of the elements of the list using the equals operator.

The == operator is used to test the equality of two objects. It returns the result as True or False.

The following program illustrates the same-

 #creating a function
 def comp_list(l1,l2):
     if(l1==l2):
         print("Both lists are equal")
     else:
         print("Both lists are not equal")
 l1=[1,2,3,4,5]
 l2=[2,4,5,1,3]
 comp_list(l1, l2)  #calling the function
 l3=[11,12,13,14,15]
 l4=[15,11,13,12,14]
 comp_list(l3, l4)  #calling the function
 l5=[20,21,22,23,24]
 l6=[20,21,22,23,24]
 comp_list(l5,l6)   #calling the function 

INPUT-

How to Compare two Lists in Python?

OUTPUT-

How to Compare two Lists in Python?

We can observe the following things-

  1. First, the two lists are passed l1 and l2 in the function comp_list(), and then the results are checked on the execution of our code.
  2. Similarly, we specified the elements in the lists l3,l4,l5, and l6 and then passed l3 and l4 in the function and then l5 and l6.
  3. In the output, we can observe that the elements of l1 and l2, l3 and l4 are not equal whereas elements of l5 and l6 are equal.

So, in this article, we discussed the various methods of comparing the elements of a list in Python.


Related Topics

StandardScaler in Sklearn

When and How Should You Use StandardScaler? StandardScaler comes into play whenever the properties of the provided dataset change significantly within their ranges or are recorded in different measurement units. Since using...

4 minutes read.

Python Program to find the length of a string

Python program to find the length of a string A string in Python is a set of Unicode characters. It can't be changed once it's been declared. The number of characters...

2 minutes read.

Falcon Python

Introduction of Python Python is the fastest and the smartest programming language and it is an object oriented language. Python has libraries that can be importedeasily and perform many operations; to...

3 minutes read.

How to import numy in python

How to Install NumPy in Python Python is a vast ocean of libraries, modules, and different functions. It has a solution for almost everything. Using Python, we can simplify even a...

3 minutes read.

Python round() function

Python round() function The round() function in Python returns a number rounded to ‘ndigits’ precision after the decimal point. If ndigits is omitted or is None, it returns the nearest integer to its input. Syntax round(number[, ndigits]) Parameter Number: This parameter represents the...

1 minute read.

Recursive Multiplication Python

Recursive Multiplication Python In this tutorial, we will learn how to write a Python program to get the multiplication of two numbers using the recursive approach. In the recursive multiplication approach, we...

2 minutes read.

Python Function

Python Function A Python function is a reusable, organized block of code that is used to perform the specific task. The functions are the appropriate way to divide an extensive program into a...

7 minutes read.

How to create a DataFrames in Python

How to create a DataFrames in Python Data Frame is a data structure in which data is stored in tabular form. They can also be referred as two-dimensional collection of data. Various...

5 minutes read.

Python program to find the GCD or HCF

Python program to find the GCD or HCF The largest positive integer that perfectly divides the two numbers is the HCF (Highest Common Factor) or GCD (Greatest Common Deviser). For example,...

5 minutes read.

Python bin() function

Python bin() function The bin() function in Python returns the binary version for the specified integer. Syntax bin(x) Parameter n: This parameter represents an integer or int value. Return This function returns a binary string for the specified integer...

1 minute read.

How to Install Matplotlib in Python?

How to Install Matplotlib in Python The speed at which the enormous amount of data is generating has become a huge aid in understanding what's going on currently in the market....

4 minutes read.

Continue and Pass Statements in Python

Difference between continue and pass statements in Python The tasks can be repeated and automated efficiently using loops in Python. Sometimes we have to exit from the entire loop completely, we...

2 minutes read.

Python Word Tokenizer

Python Overview Python is an Object-Oriented high-level language. Python is designed to be highly beginner-friendly. Python has an English-like syntax, which is very easy to read. Python is an interpreted language...

4 minutes read.

Python Variables, Constants and Literals

Python is today's most valuable, easy-to-implement and sought-out programming language. Nowadays, developers want to focus on implementing the program rather than spending time with complex programs. So, for this reason,...

17 minutes read.

Python List clear() method

Python List clear() method The list.clear () method in Python extends the list by appending all the items from the iterable. Syntax list.clear() Parameter NA Example 1 # Python Program explaining # the list.clear() method week_list = ["Monday", "Tuesday", "Wednesday","Thursday","Friday",...

1 minute read.

Data Drop in Python

Introduction You'll understand how to delete a group of rows from a Pandas dataframe in this article.You can read this article on How to Drop Columns in Pandas to find out...

6 minutes read.

Python Goto Statement

We all know that Python is the most basic and widely used programming language in the world. It is also one of the world's most popular and widely used languages....

4 minutes read.

Python program to find whether a given number is even or odd

Python program to find whether a given number is even or odd A number is said to be even if any number is completely divisible by 2, which means there is...

1 minute read.

Python Continue Statement

In Python, loops automate and repeat processes in a cost-effective manner. However, there may be occasions when you wish to entirely exit the loop, skip an iteration, or ignore the...

3 minutes read.

Python program to find factorial of a given number

Python program to find factorial of a given number Factorial is a non-negative integer. It is the product of all positive integers from 1 to the number we are going to...

3 minutes read.