[ python-Bugs-1095342 ] General FAQ: list.sort() out of date

SourceForge.net noreply at sourceforge.net
Sat Jan 8 13:41:16 CET 2005


Bugs item #1095342, was opened at 2005-01-04 00:16
Message generated for change (Comment added) made by jlgijsbers
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1095342&group_id=5470

Category: None
Group: None
>Status: Closed
>Resolution: Accepted
Priority: 5
Submitted By: Tim Delaney (tcdelaney)
>Assigned to: Johannes Gijsbers (jlgijsbers)
Summary: General FAQ: list.sort() out of date

Initial Comment:
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]...


----------------------------------------------------------------------

>Comment By: Johannes Gijsbers (jlgijsbers)
Date: 2005-01-08 13:41

Message:
Logged In: YES 
user_id=469548

Fixed in rev 1.22 of general.ht. Thanks for the new text!

----------------------------------------------------------------------

Comment By: Tim Delaney (tcdelaney)
Date: 2005-01-04 01:28

Message:
Logged In: YES 
user_id=603121

Updated text:

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 a 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]...

Versions of Python prior to 2.4 need to use the following idiom:

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


----------------------------------------------------------------------

Comment By: Tim Delaney (tcdelaney)
Date: 2005-01-04 00:21

Message:
Logged In: YES 
user_id=603121

Do we want to also reference the 2.3 and earlier idiom?

----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1095342&group_id=5470


More information about the Python-bugs-list mailing list