test for list equality

Tim Chase python.list at tim.thechases.com
Thu Dec 15 13:20:10 EST 2011


On 12/15/11 11:59, Miki Tebeka wrote:
>> My sort issue... as in this doesn't work
>>>>> if x.sort == y.sort:
> You're missing the () to make it a function call.
> Also list.sort() returns none, it mutates the original list.
> You can either
>      sorted(x) == sorted(y)
> or
>      set(x) == set(y)

Duplicates cause issues in the set() version:

   a = [1,2,3,4]
   b = a + a
   print sorted(a) == sorted(b) # False
   print set(a) == set(b) # True

They mean different things, and the OP may want one or the other 
depending on how they want to consider duplicates.

-tkc






More information about the Python-list mailing list