converting a set into a sorted list

Robert Kern rkern at ucsd.edu
Sat May 14 22:20:24 EDT 2005


MackS wrote:
> Dear all,
> 
> I've got several large sets in my program. After performing several
> operations on these I wish to present one set to the user [as a list]
> sorted according to a certain criterion. Is there any direct way to do
> so? Or must I
> 
> list = []
> 
> for item in set1:
>    list.append(item)
> 
> list.sort(....)
> 
> Can I somehow avoid doing this in two stages? Can I somehow avoid first
> creating a long list only to immediately sort it afterwards?

In Python 2.4,

In [1]:sorted?
Type:           builtin_function_or_method
Base Class:     <type 'builtin_function_or_method'>
String Form:    <built-in function sorted>
Namespace:      Python builtin
Docstring:
     sorted(iterable, cmp=None, key=None, reverse=False) --> new sorted list

-- 
Robert Kern
rkern at ucsd.edu

"In the fields of hell where the grass grows high
  Are the graves of dreams allowed to die."
   -- Richard Harter




More information about the Python-list mailing list