converting a set into a sorted list

Bengt Richter bokr at oz.net
Sun May 15 03:42:35 EDT 2005


On Sat, 14 May 2005 19:20:24 -0700, Robert Kern <rkern at ucsd.edu> wrote:

>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
>
That's plenty of information, but IMO "key=None" doesn't hint strongly enough
about what you can do with it, so I'd advise reading about all the parameters ;-)

Regards,
Bengt Richter



More information about the Python-list mailing list