Lostsoul
Lostsoul

Reputation: 25999

Is there a way to see if two lists are the exact same without looping in python?

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

Answers (3)

Oli4
Oli4

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

zellio
zellio

Reputation: 32484

the == operator should function as expected on lists

>>> x = [1, 2]  
>>> y = [1, 2]
>>> x == y
True

Upvotes: 3

icktoofay
icktoofay

Reputation: 129011

Doesn't == work?

>>> a = [1, 2, 3]
>>> b = [1, 2, 3]
>>> a == b
True

Upvotes: 11

Related Questions