problem with sorting

jwelby julius.welby at gmail.com
Fri Mar 28 07:44:31 CET 2008


On Mar 28, 5:38 am, ankitks.mi... at gmail.com wrote:
> >>> dict = {'M':3, 'R':0, 'S':2}
> >>> print dict
>
> {'S': 2, 'R': 0, 'M': 3}
>
> now if I wanted sorted values in list, i am not able to do this>>> print dict.values().sort()
>
> None
>
> it returns None instead of [0, 2, 3]

The sort method works by sorting 'in place'. That means it doesn't
return the sorted value, but just sorts the sequence.

>>> t = {'M':3, 'R':0, 'S':2}
>>> x = t.values()
>>> x.sort()
>>> x
[0, 2, 3]

or you can use sorted(), which does return the sorted sequence:

>>> sorted(t.values())
[0, 2, 3]



More information about the Python-list mailing list