problem with sorting

Duncan Booth duncan.booth at invalid.invalid
Fri Mar 28 06:53:54 EDT 2008


castironpi at gmail.com wrote:

> On Mar 28, 1:57ÿam, raj <rajeeshrn... at gmail.com> wrote:
>> To ankit:
>>
>> Well, sort() doesn't return the sorted list. It returns None. Why not
>> this straightforward way?
>> dvals = dict.values()
>> dvals.sort()
>> print dvals
> 
> Why not sorted( dict.values() ).
> 

If you are going to do it that way then it may be preferable to use 
itervalues:

   print sorted(dict.itervalues())

Both this and raj's suggestion create a single sorted list. Your suggestion 
creates two lists: the unsorted one and a separate sorted one. In most 
cases the difference is probably insignificant, but if you have a *lot* of 
values it might make a difference.



More information about the Python-list mailing list