Reputation: 25999
Say I have:
a = [1, 2, 3]
b = [1, 2, 3]
is there a way to test the lists to see if they are the same, without having to loop through each entry?
Here's what I was thinking..I know to check if two variables are the same I could use:
id(a)
but it doesn't work because the ID's are different so is there some type of checksum or way that python stores values of the table so I can simply compare two variables?
Upvotes: 3
Views: 1603
Reputation: 499
the == operator should function works on lists
This output is received on certain versions of python, I don't know which.
>>> import numpy as np
>>> x = [1, 2, 3, 4]
>>> y = [1, 2, 3, 4]
>>> z = [1, 2, 2, 4]
>>> x == y
[True, True, True, True]
>>> x == z
[True, True, False, True]
After this, just use numpy to determine the entire list.
>>> np.all(x == y)
True
>>> np.all(x == z)
False
or if only a single similarity is required:
>>> np.any(x == z)
True
Upvotes: 0
Reputation: 32484
the ==
operator should function as expected on lists
>>> x = [1, 2]
>>> y = [1, 2]
>>> x == y
True
Upvotes: 3
Reputation: 129011
Doesn't ==
work?
>>> a = [1, 2, 3]
>>> b = [1, 2, 3]
>>> a == b
True
Upvotes: 11