Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Python: Bugs

[issue7257] Improve documentation of list.sort and sorted()

 

 

Python bugs RSS feed   Index | Next | Previous | View Threaded


report at bugs

Nov 3, 2009, 10:03 AM

Post #1 of 7 (253 views)
Permalink
[issue7257] Improve documentation of list.sort and sorted()

New submission from Ole Laursen <olau [at] iola>:

On my Python 3.1, help() for sorted returns

sort(...)
L.sort(key=None, reverse=False) -- stable sort *IN PLACE*

sorted(...)
sorted(iterable, key=None, reverse=False) --> new sorted list

Kindly suggest this be expanded. Here's some text:

sort(...)

Sorts the sequence with a fast stable sort. The sequence is modified in
place. To remind you of this, the function always returns None. Example:

a = [1, 3, 2]
a.sort()
# a is now [1, 2, 3]

Use the "sorted()" built-in function if you need to preserve the
original list.

Set "reverse" to True to sort the elements in reverse order. A function
for extracting a key for comparison from each object can be passed in as
"key", e.g.

a = [{'k': 'foo'}, {'k': 'bar'}]
a.sort(key=lambda x: x['k'])
# a is now [{'k': 'bar'}, {'k': 'foo'}]

Note that "key" can be used to solve many sorting problems, e.g.
key=str.lower can be used for case-insensitive sorting and key=lambda x:
(x['a'], x['b']) can be used to sort by first 'a' then 'b'.

The sort is stable which means that the relative order of elements that
compare equal is not changed.


sorted(...)

Sorts the sequence with a fast stable sort and returns a new list with
the result. Example:

[same text as before]


I'm not sure how this interacts with what's in the online help
(http://docs.python.org/3.1/library/stdtypes.html search for "sort("),
maybe the text could just be copied over. I think it's important to give
copy-pasteable examples for something as important as this, and hint at
how you solve common sorting problems.

----------
assignee: georg.brandl
components: Documentation
messages: 94863
nosy: georg.brandl, olau
severity: normal
status: open
title: Improve documentation of list.sort and sorted()

_______________________________________
Python tracker <report [at] bugs>
<http://bugs.python.org/issue7257>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com


report at bugs

Nov 3, 2009, 10:14 AM

Post #2 of 7 (232 views)
Permalink
[issue7257] Improve documentation of list.sort and sorted() [In reply to]

Georg Brandl <georg [at] python> added the comment:

Raymond, do you have an opinion on this?

----------
assignee: georg.brandl -> rhettinger
nosy: +rhettinger

_______________________________________
Python tracker <report [at] bugs>
<http://bugs.python.org/issue7257>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com


report at bugs

Nov 3, 2009, 10:22 AM

Post #3 of 7 (235 views)
Permalink
[issue7257] Improve documentation of list.sort and sorted() [In reply to]

Raymond Hettinger <rhettinger [at] users> added the comment:

I'm inclined to leave the on-line help docstring as-is (pretty much
everywhere, we adopt a style of terse reminders instead of lengthy prose
with examples).

Instead, was thinking of updating the sorting how-to and providing a
link to it from the main docs.

----------
priority: -> low

_______________________________________
Python tracker <report [at] bugs>
<http://bugs.python.org/issue7257>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com


report at bugs

Nov 3, 2009, 11:22 AM

Post #4 of 7 (243 views)
Permalink
[issue7257] Improve documentation of list.sort and sorted() [In reply to]

Ole Laursen <olau [at] iola> added the comment:

If you think it's too long, here's a shorter version:

Sorts sequence in place with a fast stable sort, returning None. key is
a function for extracting a comparison key from each element, e.g.
key=lambda x: x['name'] or key=str.lower. reverse=True reverses the
result order.

a = [1, 3, 2]
a.sort() # a is now [1, 2, 3]

Use the "sorted()" built-in function if you need to preserve the
original list.


Is this better? You could whack the last comment about sorted and/or the
example, then it's about the same length as the other built-in
docstrings (e.g. zip, reduce, getattr).

----------
nosy: -rhettinger

_______________________________________
Python tracker <report [at] bugs>
<http://bugs.python.org/issue7257>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com


report at bugs

Nov 3, 2009, 11:45 AM

Post #5 of 7 (234 views)
Permalink
[issue7257] Improve documentation of list.sort and sorted() [In reply to]

Changes by Georg Brandl <georg [at] python>:


----------
nosy: +rhettinger

_______________________________________
Python tracker <report [at] bugs>
<http://bugs.python.org/issue7257>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com


report at bugs

Nov 3, 2009, 11:49 AM

Post #6 of 7 (232 views)
Permalink
[issue7257] Improve documentation of list.sort and sorted() [In reply to]

Raymond Hettinger <rhettinger [at] users> added the comment:

Will look at it and make an update, but not right away.

----------

_______________________________________
Python tracker <report [at] bugs>
<http://bugs.python.org/issue7257>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com


report at bugs

Nov 4, 2009, 2:11 AM

Post #7 of 7 (216 views)
Permalink
[issue7257] Improve documentation of list.sort and sorted() [In reply to]

Ole Laursen <olau [at] iola> added the comment:

OK, thanks! :) Sorry about the unintended nosy list removal, my browser
got me there.

----------

_______________________________________
Python tracker <report [at] bugs>
<http://bugs.python.org/issue7257>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com

Python bugs RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact Gossamer Threads
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.