Change language

Python | Check if the two lists are the same

|

Method 1: Using list.sort () and the == operator
sort () combined with the == operator can do this. First, we sort the list so that if both lists are identical, then they have elements at the same position. But that doesn’t take into account the order of the elements in the list.

# Python 3 demo code
# check if the lists are identical
# using sort () + ==

 
# initializing lists

test_list1 = [ 1 , 2 , 4 , 3 , 5 ]

test_list2 = [ 1 , 2 , 4 , 3 , 5 ]

 
# print lists

print ( "The first list is:" + str (test_list1))

print ( "The second list is:" + str (test_list2))

 
# sort both lists
test_list1.sort ()
test_list2. sort ()

 
# using == to check
# lists are equal

if test_list1 = = test_list2:

print ( "The lists are identical " )

else :

print ( "The lists are not identical" )

Output:

 The first list is: [1, 2, 4, 3, 5] The second list is: [1, 2, 4, 3, 5] The lists are identical  

Method 2: Using collections.Counter()
Using Counter () we can usually get the frequency of each element in the list by checking it, for both lists we can check if the two lists are identical or not. But this method also ignores the order of the elements in the list and only considers the frequency of the elements.

# Python 3 demo code
# check if the lists are identical
# use collection.Counter ()

import collections

  
# initializing lists

test_list1 = [ 1 , 2 , 4 , 3 , 5 ]

test_list2 = [ 1 , 2 , 4 , 3 , 5 ]

 
# printing lists

print ( "The first list is: " + str (test_list1))

print ( "The second list is:" + str (test_list2))

 
# using collection.Counter () to check
# the lists are equal

if collections.Counter (test_list1) = = collections.Counter (test_list2):

print ( "The lists are identical" )

else :

print ( "The lists are not identical" )

Output:

 The first list is: [1, 2, 4, 3, 5] The second list is: [1, 2, 4, 3, 5] The lists are identical 

Spos vol 3: using sum () + zip () + len ()
Using sum () + zip () we can get the sum one list as a sum of 1 if both indices in the two lists have the same elements, then compare that number with the size of the other list. It also requires you to first check if the two lists are equal before doing this computation. It also checks the order.

# Python 3 demo code
# check if they are identical lists
# using sum () + zip () + len ()

 
# initializing lists

test_list1 = [ 1 , 2 , 4 , 3 , 5 ]

test_list2 = [ 1 , 2 , 4 , 3 , 5 ]

 
# print lists

print ( "The first list is:" + str (test_list1))

print ( "The second list is:" + str (test_list2))

 
# using sum () + zip () + len () to check
# the lists are equal

i f len (test_list1) = = len (test_list2) and len (test_list1) = = sum ([ 1 for i, j in zip (test_list1, test_list2) if i = = j]):

print ( "The lists are identical"

else :

print ( " The lists are not identical " )

Output:

 The first list is: [1, 2, 4, 3, 5] The second list is: [1, 2, 4, 3, 5] The lists are identical 

Method 4: using reduce () + map ()
Tying the cardinality carefully map () with hash values ​​and the utility of reduce () , we can perform the task of checking if two lists are equal. This also respects the order of the list.

# Python 3 demo code
# check if they are identical whether lists
# using map () + decrease ()

import functools

 
# initializing lists

test_list1 = [ 1 , 2 , 4 , 3 , 5 ]

test_list2 = [ 1 , 2 , 4 , 3 , 5 ]

 
# print lists

print ( "The first list is:" + str (test_list1))

print ( " The second list is: " + str (test_list2))

 
# using map () + lower () to check
  # lists are equal

if functools. reduce ( lambda i, j: i and j, map ( lambda m, k: m = = k, test_list1, test_list2), True ): 

print ( "The lists are identical" )

else :

  print "The lists are not identical" )

Output:

 The first list is: [1, 2, 4, 3, 5] The second list is: [1, 2, 4, 3, 5] The lists are identical 

Shop

Learn programming in R: courses

$

Best Python online courses for 2022

$

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Latest questions

NUMPYNUMPY

Common xlabel/ylabel for matplotlib subplots

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

12 answers

NUMPYNUMPY

Flake8: Ignore specific warning for entire file

12 answers

NUMPYNUMPY

glob exclude pattern

12 answers

NUMPYNUMPY

How to avoid HTTP error 429 (Too Many Requests) python

12 answers

NUMPYNUMPY

Python CSV error: line contains NULL byte

12 answers

NUMPYNUMPY

csv.Error: iterator should return strings, not bytes

12 answers

News


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

sin

How to specify multiple return types using type-hints

exp

Printing words vertically in Python

exp

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

cos

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically