×

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

FOO in Python

Python Programming Language: Python programming language is one of the most used programming languages, as it is used widely in the field of software and data analysis, web development, etc. It...

3 minutes read.

An Introduction to Mocking in Python

Python Mocking is a testing library. It enables you to substitute portions of your system under test with fake(mock) objects and make assertions about how they were utilized.  The test is...

3 minutes read.

GUI to extract lyrics from a song Using Tkinter in Python

GUI: A graphical interface (GUI) is a user interface that lets users interact with electronic devices like computers and smartphones by using menus, icons, and other visual cues (graphics). In contrast...

4 minutes read.

How to read data from com port in python

Comport, the I/O interface is known as a COM port that allows the connection for a serial device to a computer. COM ports are sometimes referred to as serial ports....

3 minutes read.

How to reverse a string in Python

In Python language, we have a few ways to reverse a string. In this article, let us discuss these ways and understand the suitability of these ways in different scenarios...

5 minutes read.

SKLearn Clustering

These are ml methods thatare responsible for detecting patterns and the similarities within the data.The clustering methods are unsupervised.Here the data is clustered to form groups with the help of...

3 minutes read.

Python Random shuffle( ) method

The shuffle() is used to change the positions of the elements in the mutable sequences. The shuffle( ) function will change the positions of the elements in the sequence of...

3 minutes read.

Python String expandtabs() method

Python String expandtabs() method The string.expandtabs() method in Python returns a copy of the string where all tab characters are expanded using spaces. Syntax String.expandtabs([tabsize]) Parameter tabsize: This parameter specifies the number of characters to...

1 minute read.

Applications of Python

Top 10 Applications of Python in 2020- 2021 Python language is famous for its general-purpose nature, which enables developers to use it in every field of development. Python can be found...

6 minutes read.

Python String lower() method

Python String lower() method The string.lower() method in Python returns a string where all characters are lower case. Syntax string.lower() Parameter NA Return This method returns a string where all characters are lower case. Example 1 # Python...

1 minute 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.

Python Arithmetic Operators

An arithmetic operator is a mathematical operator that is used to operate on two operands. Based on the operator used, action is performed on the operands, and output is delivered. Following...

3 minutes read.

Python Permutations and Combinations

In mathematics, we all studied what is meant by permutations and combinations. “Permutations” define the way of arranging the elements in sequential order. “Combinations” mean the way of selecting the...

7 minutes read.

How to declare array in python

How to declare array in python? Arrays are a great way of storing our numeric values into a variable in continuous locations. For declaring an array in C language, we used...

4 minutes read.

Python List reverse() method

 Python List reverse() method The list.reverse () method in Python reverses the elements of the list in place. Syntax list.reverse() Parameter NA Example 1 # Python program explaining # the list.reverse() method weekList = ['Sun','Thurs','Mon','Fri', 'Tues', 'Wed'] print("Actual week list: ",weekList) #...

1 minute read.

Python program to print array element present at even position

Python program to print array element present at even position In this program, we'll see a Python program that prints the elements of an array that are in even positions. We...

1 minute read.

Introduction to Scratch programming

Scratch programming is generally designed for children who may create digital stories, games, and animations using the computer language Scratch, which has the largest kid-focused community in the world. Scratch...

3 minutes read.

How to print in the same line in Python?

How to print in the same line in python By default, the print function in Python takes us to the next line and prints the desired statement in the output. In this...

5 minutes read.

Python Program to check whether a given number is Armstrong or not

Program to check whether a given number is Armstrong or not A number is said to be an Armstrong if the sum of each digit's cube of a given number equals...

1 minute read.

How to learn python Online

Need to Learn a Programming Language 1. To get a high Paying job We know that Software Engineering is one of the top-paying professions in the world. The top criteria to be...

3 minutes read.