Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

General FAQ: list.sort() out of date #41392

Closed
tcdelaney mannequin opened this issue Jan 3, 2005 · 4 comments
Closed

General FAQ: list.sort() out of date #41392

tcdelaney mannequin opened this issue Jan 3, 2005 · 4 comments

Comments

@tcdelaney
Copy link
Mannequin

tcdelaney mannequin commented Jan 3, 2005

BPO 1095342

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = None
closed_at = <Date 2005-01-08.12:41:15.000>
created_at = <Date 2005-01-03.23:16:48.000>
labels = []
title = 'General FAQ: list.sort() out of date'
updated_at = <Date 2005-01-08.12:41:15.000>
user = 'https://bugs.python.org/tcdelaney'

bugs.python.org fields:

activity = <Date 2005-01-08.12:41:15.000>
actor = 'jlgijsbers'
assignee = 'jlgijsbers'
closed = True
closed_date = None
closer = None
components = ['None']
creation = <Date 2005-01-03.23:16:48.000>
creator = 'tcdelaney'
dependencies = []
files = []
hgrepos = []
issue_num = 1095342
keywords = []
message_count = 4.0
messages = ['23861', '23862', '23863', '23864']
nosy_count = 2.0
nosy_names = ['jlgijsbers', 'tcdelaney']
pr_nums = []
priority = 'normal'
resolution = 'accepted'
stage = None
status = 'closed'
superseder = None
type = None
url = 'https://bugs.python.org/issue1095342'
versions = []

@tcdelaney
Copy link
Mannequin Author

tcdelaney mannequin commented Jan 3, 2005

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

@tcdelaney tcdelaney mannequin closed this as completed Jan 3, 2005
@tcdelaney tcdelaney mannequin assigned jlgijsbers Jan 3, 2005
@tcdelaney tcdelaney mannequin closed this as completed Jan 3, 2005
@tcdelaney tcdelaney mannequin assigned jlgijsbers Jan 3, 2005
@tcdelaney
Copy link
Mannequin Author

tcdelaney mannequin commented Jan 3, 2005

Logged In: YES
user_id=603121

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

@tcdelaney
Copy link
Mannequin Author

tcdelaney mannequin commented Jan 4, 2005

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

@jlgijsbers
Copy link
Mannequin

jlgijsbers mannequin commented Jan 8, 2005

Logged In: YES
user_id=469548

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

@ezio-melotti ezio-melotti transferred this issue from another repository Apr 9, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

0 participants