Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (50.2k points)
Can anyone tell me how to compare two lists in Python?

1 Answer

0 votes
by (108k points)

For comparing lists in Python, what you need to do is, first, declare the two lists, sort all the elements in the lists, and then compare the elements using the if condition. Refer to the below code:

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

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

  

# sorting both the lists 

list1.sort() 

list2.sort() 

  

# using == to check if  

# lists are equal 

if list1 == list2: 

    print ("The lists are identical") 

else : 

    print ("The lists are not identical") 

The output:

The lists are identical

If you are looking for an online course to learn Python, I recommend this Python Course by Intellipaat.

Related questions

0 votes
1 answer
0 votes
1 answer
+2 votes
3 answers

Browse Categories

...