On Thu, Feb 27, 2014 at 07:24:24AM +0100, ast wrote: > Hello > > box is a list of 3 integer items > > If I write: > > box.sort() > if box == [1, 2, 3]: > > > the program works as expected. But if I write: > > if box.sort() == [1, 2, 3]: > > it doesn't work, the test always fails. Why ? > > Thx > -- > https://mail.python.org/mailman/listinfo/python-list
Because when you call the .sort() method on a list, it does the sort in-place, instead of returning a sorted copy of the list. Check this: >>> [2,1,3].sort() >>> The method does not return a value, that's why the direct comparison fails. What you might want is to use the sorted() method on the list, like this: >>> sorted([2,1,3]) [1, 2, 3] >>> sorted([2,1,3]) == [1,2,3] True -- Eduardo Alan Bustamante López -- https://mail.python.org/mailman/listinfo/python-list