473,396 Members | 1,774 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

problem with sorting

>>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]

Mar 28 '08 #1
5 1002
On Mar 28, 5:38 am, ankitks.mi...@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]
Try:

from pprint import pprint as pp
pp(my_dict)
It works well for other built-in types too.

P.S it is not good to use a name, dict, that already has a use in
Python.

- Paddy.
Mar 28 '08 #2
On Mar 28, 5:38 am, ankitks.mi...@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]
Mar 28 '08 #3
raj
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
Mar 28 '08 #4
ca********@gmail.com wrote:
On Mar 28, 1:57˙am, raj <rajeeshrn...@gmail.comwrote:
>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.
Mar 28 '08 #5
Duncan Booth:
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.
The good thing of Python 3.0 is that it forces you to do the right
thing here :-)

Bye,
bearophile
Mar 28 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: jwedel_stolo | last post by:
Hi I'm creating a dataview "on the fly" in order to sort some data prior to writing out the information to a MS SQL table I have used two methods in order to determine the sort order of the...
20
by: Xah Lee | last post by:
Sort a List Xah Lee, 200510 In this page, we show how to sort a list in Python & Perl and also discuss some math of sort. To sort a list in Python, use the “sort” method. For example: ...
7
by: Jean-Francois.Doyon | last post by:
Hello, I'm trying to retrieve a limited number of random rows, and order them by a column, and am not having any luck with that last part: SELECT * FROM tablename ORDER BY random(), id LIMIT...
4
by: FBM | last post by:
Hi, I am working on a program that simulates one of the elements of ATM. The simulation stores events which occurs every some milliseconds for a certain amount of time. Every time that an event...
7
by: Fernando Barsoba | last post by:
Hi, After following the advice received in this list, I have isolated the memory leak problem I am having. I am also using MEMWATCH and I think it is working properly. The program does some...
7
by: Kamal | last post by:
Hello all, I have a very simple html table with collapsible rows and sorting capabilities. The collapsible row is hidden with css rule (display:none). When one clicks in the left of the...
1
by: =?Utf-8?B?YmJkb2J1ZGR5?= | last post by:
I have a grid view that pulls data from a dbf file. I set the Allow Sorting to true and I put my code in the Sorting event. The problem is that I can't get the sorting to work so I wrote some...
7
by: abracadabra | last post by:
I am reading an old book - Programming Pearls 2nd edition recently. It says, "Even though the general C++ program uses 50 times the memory and CPU time of the specialized C program, it requires...
1
by: Ahmed Yasser | last post by:
Hi all, i have a problem with the datagridview sorting, the problem is a bit complicated so i hope i can describe in the following steps: 1. i have a datagridview with two columns...
1
by: sheldonlg | last post by:
I have inherited code with a TDC control. In this file, there are two javascripts of interest. One of these is a function, filter(), which is inside <script language=javascript></script>. The...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.