This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author tcdelaney
Recipients
Date 2005-01-03.23:16:48
SpamBayes Score
Marked as misclassified
Message-id
In-reply-to
Content
http://www.python.org/doc/faq/general.html#why-
doesn-t-list-sort-return-the-sorted-list

specifies the idiom:

keys = dict.keys()
keys.sort()
for key in keys:
        ...do whatever with dict[key]...

and doesn't mention sorted().

I would suggest the following wording be used:

In situations where performance matters, making a copy 
of the list just to sort it would be wasteful. Therefore, 
list.sort() sorts the list in place. In order to remind you 
of that fact, it does not return the sorted list. This way, 
you won't be fooled into accidentally overwriting a list 
when you need a sorted copy but also need to keep the 
unsorted version around.

In Python 2.4 a new builtin - sorted() - has been added. 
This function creates a new list from the passed 
iterable, sorts it and returns it.

As a result, here's the idiom to iterate over the keys of a 
dictionary in sorted order:

for key in sorted(dict.iterkeys()):
        ...do whatever with dict[key]...
History
Date User Action Args
2007-08-23 14:28:49adminlinkissue1095342 messages
2007-08-23 14:28:49admincreate