sorted (WAS: lambda)
steven.bethard at gmail.com
Thu Jan 13 19:28:27 CET 2005
Paul Rubin wrote:
> That completely depends on the objects in question. Compare
> temp = all_posters[:]
> top_five_posters = temp[-5:]
> top_five_posters = all_posters.sorted()[-5:]
> which became possible only when .sorted() was added to Python 2.4.
I believe you mean "when sorted() was added to Python 2.4":
py> ['d', 'b', 'c', 'a'].sorted()
Traceback (most recent call last):
File "<interactive input>", line 1, in ?
AttributeError: 'list' object has no attribute 'sorted'
py> sorted(['d', 'b', 'c', 'a'])
['a', 'b', 'c', 'd']
Note that sorted is a builtin function, not a method of a list object.
It takes any iterable and creates a sorted list from it. Basically the
result = list(iterable)
More information about the Python-list