walterbyrd a écrit : >> Did you try to sort a tuple ? >> >> >>> (1, "aaa").sort() >> Traceback (most recent call last): >> File "<stdin>", line 1, in ? >> AttributeError: 'tuple' object has no attribute 'sort' > > I can do this: > >>>> x = (3,2,1) >>>> x = tuple(sorted(list(x))) > > Which, although senseless, effectively sorts the x tuple.
Err... > But, you are > right, I am not really sorting the tuple, Indeed. > I'm sorting a list, which > has been made from the tuple, then changing it back to a tuple. Exactly. You're in fact *creating* a new tuple - how you do it is somewhat irrelevant. This is certainly not the same thing as in-place sorting. From a purely practical POV, using tuples as "frozen lists" can sometimes make sens - be it for micro-optimization (tuples are cheaper than lists). But still, you'll find that in most cases(with most==almost always), tuples are used for (eventually heterogenous) data *where the order is significant* (fields in a db row, argument list, base classes lists, 2D or 3D points, etc), and can not be changed without changing the semantic of the data, while lists are used to collect (more or less) homogenous data where the order doesn't impact the semantic of the data (which is one of the reasons why list can be sorted and tuples cannot). As a matter of fact, a dict can be build from a list of (key,value) tuples - and it's obvious that the order of the elements in each tuple is significant, while the order of tuples in the list is not. HTH -- http://mail.python.org/mailman/listinfo/python-list