How to check List Equality in Python

Hi guys. How are you all? I hope you all fine. Today in this tutorial we will learn about How to check List Equality in Python. Here i am giving you some methods which will be useful to check if the given two or three list are equal or not. So lets learn to check this by given below methods. I hope you all like it and this will help you.

How to check List Equality in Python

  1. Check List Equality in Python

    to check List Equality in Python just Use == operator. By using == operator you can check if the two list are same or not. You can check the equality of your list. So first of all make the lists you want to check. Then use the == operator and print the lists. By this you can check if the lists are equal or not. So Lets learn this by given below example. I hope you like it and will help you. list1 = [11,15,19,14,17] list2 = [14,17,13,15,18] list3 = [11,15,19,14,17] print(list1 == list2 ) print(list1 == list3 ) Output : False True

  2. How to check List Equality in Python

    to check List Equality in Python just Use np.array(). You can check the equality of the list by using np.array() function. For this first of all you have to import numpy and then take the lists you want to check the equality off. Then you have to use np.array() function with the lists and also have to use == operator and then print it> by this you can check the equality of the lists. So lets learn this by given below example. I hope you guys like it. import numpy as np list1 = [11,15,19,14,17] list2 = [14,17,13,15,18] list3 = [11,15,19,14,17] list4 = [11,16,19,15,17] print((np.array(list1) == np.array(list2))) print((np.array(list1) == np.array(list3))) print((np.array(list3) == np.array(list4))) Output :[False False False False False] [ True True True True True] [ True False True False True]

Method 1: Use == operator

By using == operator you can check if the two list are same or not. You can check the equality of your list. So first of all make the lists you want to check. Then use the == operator and print the lists. By this you can check if the lists are equal or not. So Lets learn this by given below example. I hope you like it and will help you.

list1 = [11,15,19,14,17]
list2 = [14,17,13,15,18]
list3 = [11,15,19,14,17]
print(list1 == list2 )
print(list1 == list3 )

Output :

False
True

Method 2: Use np.array()

You can check the equality of the list by using np.array() function. For this first of all you have to import numpy and then take the lists you want to check the equality off. Then you have to use np.array() function with the lists and also have to use == operator and then print it> by this you can check the equality of the lists. So lets learn this by given below example. I hope you guys like it.

import numpy as np

list1 = [11,15,19,14,17]
list2 = [14,17,13,15,18]
list3 = [11,15,19,14,17]
list4 = [11,16,19,15,17]
print((np.array(list1) == np.array(list2)))
print((np.array(list1) == np.array(list3)))
print((np.array(list3) == np.array(list4)))

Output :

[False False False False False]
[ True  True  True  True  True]
[ True False  True False  True]

Method 3: Use == and numpy.all()

Byusing == and numpy.all() functions you can check the equality of the list. Here first i ahve been import numpy and then use this functions and print it. So this is very simple method. I hope this will help you. lets learn this by given below example:

import numpy as np

list1 = np.array([11,15,19,14,17])
list2 = np.array([14,17,13,15,18])
list3 = np.array([11,15,19,14,17])
list4 = np.array([11,16,19,15,17])

print((list1 == list2).all())
print((list1 == list3).all())
print((list3 == list4).all())

Output :

False
True
False

Conclusion

So This is all About This Tutorial. Hope This Tutorial Helped You. Thank You.

Also Read,

Siddharth

I am an Information Technology Engineer. I have Completed my MCA And I have 4 Year Plus Experience, I am a web developer with knowledge of multiple back-end platforms Like PHP, Node.js, Python and frontend JavaScript frameworks Like Angular, React, and Vue.

Leave a Comment