Comparing lists ...

James Stroud jstroud at mbi.ucla.edu
Tue Feb 13 19:17:49 EST 2007


Loic wrote:
> I would like to know if it is possible, and how to do this with Python:
> 
> I want to design a function to compare lists and return True only if 
> both lists are equal considering memory location of the list.
> I suppose it would be the equivalent of comparing 2 pointers in c++
> 
> lets call this function check(a, b)
> and define a few lists:
> 
>  >>> l0 = [1, 2, 3, 4]
>  >>> l1 = [1, 2, 3, 4]
>  >>> l2 = l0
> 
> I want it to have the following behaviour:
> 
>  >>> check(l1, l0)
> False
> 
>  >>> check(l2, l0)
> True
> 
>  >>> check(l1, l2)
> False
> 
> 
> Any idea how to do this with Python?

Use "is". E.g.:

l0 is l1

James



More information about the Python-list mailing list