471,338 Members | 1,491 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

Sorting dict keys

Consider the following:
>>a = {1:2, 3:4, 2:5}
Say that i want to get the keys of a, sorted. First thing I tried:
>>b = a.keys().sort()
print b
None

Doesn't work. Probably because I am actually trying to sort the keys
of the dictionary without copying them first. If that is the case,
fine. Next thing I do:
>>b = a.keys()
b.sort()
[1, 2, 3]

Works fine, but I would really like it if I could somehow do it in one
line. As the problem seems to be copying the object, i try the
following:
>>import copy
b = copy.copy(a.keys()).sort()
print b
None

Hmmm, why doesn't it work? Also,
>>b = copy.deepcopy(a.keys()).sort()
print b
None

(not that I thought that deepcopy will work since shallow didn't, I
understand the difference :) )
Obviously, I am missing something here. What am I thinking wrong? Why
doesn't copying the object work?
Thanks for all the help

Jul 20 '07 #1
9 3234
mo*********@gmail.com wrote:
Consider the following:
>>>a = {1:2, 3:4, 2:5}

Say that i want to get the keys of a, sorted. First thing I tried:
>>>b = a.keys().sort()
print b
None

Doesn't work. Probably because I am actually trying to sort the keys
of the dictionary without copying them first. If that is the case,
fine.
..sort returns None.
>>>b = a.keys()
b.sort()
[1, 2, 3]
This snippet is clearly edited, because .sort returns None:
>>a = {1:2, 3:4, 2:5}
b = a.keys()
b.sort()
b
[1, 2, 3]

The problem you're having here is that .sort returns None to indicate
that it mutates the object, rather than returning a new one. So get the
keys, sort them, and then move along.
Works fine, but I would really like it if I could somehow do it in one
line.
Why is doing it in one line a priority? Clarity is more important than
brevity.

Regardless, you can do it in one line with the sorted builtin:
>>sorted(a.keys())
[1, 2, 3]

--
Erik Max Francis && ma*@alcyone.com && http://www.alcyone.com/max/
San Jose, CA, USA && 37 20 N 121 53 W && AIM, Y!M erikmaxfrancis
We must all hang together, or, most assuredly, we will all hang
separately. -- John Hancock
Jul 20 '07 #2
mo*********@gmail.com wrote:
Consider the following:
>>>>a = {1:2, 3:4, 2:5}


Say that i want to get the keys of a, sorted. First thing I tried:

>>>>b = a.keys().sort()
print b

None

Doesn't work. Probably because I am actually trying to sort the keys
of the dictionary without copying them first. If that is the case,
fine. Next thing I do:

>>>>b = a.keys()
b.sort()

[1, 2, 3]

Works fine, but I would really like it if I could somehow do it in one
line. As the problem seems to be copying the object, i try the
following:

>>>>import copy
b = copy.copy(a.keys()).sort()
print b

None

Hmmm, why doesn't it work? Also,

>>>>b = copy.deepcopy(a.keys()).sort()
print b

None

(not that I thought that deepcopy will work since shallow didn't, I
understand the difference :) )
Obviously, I am missing something here. What am I thinking wrong? Why
doesn't copying the object work?
Thanks for all the help
sort() sorts a list in place and returns None. For a one-liner:
>>a = {1:2, 3:4, 2:5}
b = sorted(a.keys())
b
[1, 2, 3]

Duncan
Jul 20 '07 #3
On Fri, Jul 20, 2007 at 03:27:51PM -0700, mo*********@gmail.com wrote:
Consider the following:
>a = {1:2, 3:4, 2:5}

Say that i want to get the keys of a, sorted. First thing I tried:
>b = a.keys().sort()
print b
None
list's sort() method sorts the list _in_place_:
>>l = ['spam', 'eggs']
help(l.sort)
Help on built-in function sort:

sort(...)
L.sort(cmp=None, key=None, reverse=False) -- stable sort *IN PLACE*;
cmp(x, y) --1, 0, 1

That means that doesn't return a sorted version of the list you're
working with. Instead, it sorts the list itself.

If you want to return a sorted list, use (duh) sorted:
>>sorted(l)
['eggs', 'spam', 'spam']

--

[Will Maier]-----------------[wi*******@ml1.net|http://www.lfod.us/]
Jul 20 '07 #4
On 21 srp, 00:47, Duncan Smith <buzz...@urubu.freeserve.co.ukwrote:
montyphy...@gmail.com wrote:
Consider the following:
>>>a = {1:2, 3:4, 2:5}
Say that i want to get the keys of a, sorted. First thing I tried:
>>>b = a.keys().sort()
print b
None
Doesn't work. Probably because I am actually trying to sort the keys
of the dictionary without copying them first. If that is the case,
fine. Next thing I do:
>>>b = a.keys()
b.sort()
[1, 2, 3]
Works fine, but I would really like it if I could somehow do it in one
line. As the problem seems to be copying the object, i try the
following:
>>>import copy
b = copy.copy(a.keys()).sort()
print b
None
Hmmm, why doesn't it work? Also,
>>>b = copy.deepcopy(a.keys()).sort()
print b
None
(not that I thought that deepcopy will work since shallow didn't, I
understand the difference :) )
Obviously, I am missing something here. What am I thinking wrong? Why
doesn't copying the object work?
Thanks for all the help

sort() sorts a list in place and returns None. For a one-liner:
>a = {1:2, 3:4, 2:5}
b = sorted(a.keys())
b

[1, 2, 3]

Duncan
Thanks, this is what I was looking for. I know that .sort() sorts the
list in-place (the snippet I posted was written in a hurry, not pasted
-- I apologise for that). In fact, this is exactly what puzzles me.
copy.copy returns a new object:
>>copy.copy(a.keys())
[1,2,3]

Then why doesn't copy.copy(a.keys()).sort() work??
Thanks to everyone who replied!

Jul 20 '07 #5
On 20 jul, 19:34, montyphy...@gmail.com wrote:
copy.copy returns a new object:
>copy.copy(a.keys())

[1,2,3]

Then why doesn't copy.copy(a.keys()).sort() work??
It works, but you don't notice it, because you don't save a reference
to the new list.
Try this:

c = copy.copy(a.keys())
c.sort()

--
Roberto Bonvallet

Jul 20 '07 #6
On Fri, 20 Jul 2007 15:27:51 -0700, montyphyton wrote:
>>>b = a.keys()
b.sort()
[1, 2, 3]

Works fine, but I would really like it if I could somehow do it in one
line.
Why? Is the Enter key on your keyboard broken? Is there a global shortage
of newline characters that I haven't been told about? Does your employer
pay you less if you write more lines?

--
Steven.

Jul 21 '07 #7
On 21 srp, 02:31, Steven D'Aprano
<st...@REMOVE.THIS.cybersource.com.auwrote:
On Fri, 20 Jul 2007 15:27:51 -0700, montyphyton wrote:
>>b = a.keys()
b.sort()
[1, 2, 3]
Works fine, but I would really like it if I could somehow do it in one
line.

Why? Is the Enter key on your keyboard broken? Is there a global shortage
of newline characters that I haven't been told about? Does your employer
pay you less if you write more lines?

--
Steven.
I'd like to do it in one line because what I am trying to do is, after
all, a single, simple enough action. I find the suggested
b = sorted(a.keys()) much more readable than breaking it up in two
lines. In the long run, I guess it's a matter of personal taste...
Do I get payed less for more source code? Since I'm a student, I don't
get payed at all, regardless of number of lines :)

Jul 21 '07 #8
mo*********@gmail.com wrote:
I'd like to do it in one line because what I am trying to do is,
after all, a single, simple enough action.
No, it's two actions:

1. You extract a list of keys from the dict
2. you sort it
I find the suggested b = sorted(a.keys()) much more readable than
breaking it up in two lines. In the long run, I guess it's a
matter of personal taste...
Yes. You'll see, at least if you do a third thing with the list it
will start to become less well-readable.

Regards,
Björn

--
BOFH excuse #450:

Terrorists crashed an airplane into the server room, have to
remove /bin/laden. (rm -rf /bin/laden)

Jul 21 '07 #9
>From ESR's "Why python?"

b = a.keys(); b.sort()

Two statements, one line. Doesn't create an incidental copy the way
sorted does but I'm not sure how much you like to jam on one line.

Cheers,
Aaron

Jul 21 '07 #10

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

9 posts views Thread by Robin Cull | last post: by
13 posts views Thread by Paul | last post: by
6 posts views Thread by David Rasmussen | last post: by
3 posts views Thread by aking | last post: by
12 posts views Thread by jeremito | last post: by
9 posts views Thread by Wolfgang Draxinger | last post: by
3 posts views Thread by james_027 | last post: by
1 post views Thread by jim-on-linux | last post: by
reply views Thread by rosydwin | last post: by

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.