sorting dictionary keys?

J

John Smith

Hi, what's the fastest way (least amount of typing) to sort dictionary
objects by the key? What's the fastest way of iterating over sorted keys
for a dictionary in terms of performance? Are the two equivalent?

The way I have been doing it is:

d = {}
#populate d
keys = d.keys()
keys.sort()
for k in keys:
print k, d[k]

but I would like to do:
#error here
for k in d.keys().sort():
print k, d[k]

why doesn't the nested function call work? Thanks in advance.
 
J

Jay O'Connor

John said:
> Hi, what's the fastest way (least amount of typing)

Never good criteria
but I would like to do:
#error here
for k in d.keys().sort():
print k, d[k]

why doesn't the nested function call work? Thanks in advance.

sort() sorts in place and returns None
 
E

Emile van Sebille

John Smith
Hi, what's the fastest way (least amount of typing) to sort dictionary
objects by the key? What's the fastest way of iterating over sorted keys
for a dictionary in terms of performance? Are the two equivalent?

The way I have been doing it is:

d = {}
#populate d
keys = d.keys()
keys.sort()
for k in keys:
print k, d[k]

but I would like to do:
#error here
for k in d.keys().sort():

sort sorts in place and doesn't return anything. In 2.4 you'll have
the sorted method.
print k, d[k]

why doesn't the nested function call work? Thanks in advance.

The shortest thing to type? You're there with s = d.keys() and
s.sort().
 
B

Bruno Desthuilliers

Jay said:
John said:
Hi, what's the fastest way (least amount of typing)

Never good criteria
but I would like to do:
#error here
for k in d.keys().sort():
print k, d[k]

why doesn't the nested function call work? Thanks in advance.

sort() sorts in place and returns None

<rant>
Which is one of the main PITA in Python IMHO :(
(not the fact that it sort in place, the fact that it does not return self).
</rant>

Bruno
 
S

Serge Orlov

<rant>
Which is one of the main PITA in Python IMHO :(
(not the fact that it sort in place, the fact that it does not return self).
</rant>

<hint>
def sort(lst):
lst.sort()
return lst
</hint>

-- Serge
 
J

Jay O'Connor

Bruno said:
Jay said:
John said:
Hi, what's the fastest way (least amount of typing)

Never good criteria
but I would like to do:
#error here
for k in d.keys().sort():
print k, d[k]

why doesn't the nested function call work? Thanks in advance.

sort() sorts in place and returns None

<rant>
Which is one of the main PITA in Python IMHO :(
(not the fact that it sort in place, the fact that it does not return
self).
</rant>


Arguably, if that's a main PITA, that's not too bad. I like Python, but
there are other things about it that bug me more. It's a minor
annoyance and given the name, I think it makes sense. If it returned
itself or a copy, the name should probably be sorted()
 
B

Bruno Desthuilliers

Serge said:
<hint>
def sort(lst):
lst.sort()
return lst
</hint>

Which is pretty ugly and adds a useless function call.

I just can't understand *why* the BDFL made this choice.
 
P

Peter Hansen

Bruno said:
Which is pretty ugly and adds a useless function call.

I just can't understand *why* the BDFL made this choice.

What part of the many past repetitions of the reason didn't
you understand?

-Peter
 
R

Raymond Hettinger

sort() sorts in place and returns None
<rant>
Which is one of the main PITA in Python IMHO :(
(not the fact that it sort in place, the fact that it does not return self).
</rant>

Bruno


Wish granted!

Py2.4 will have a classmethod called list.sorted() that accepts any
iterable as an argument and returns a new sorted list as a result.
It can be used anywhere you can use an expression (function call
arguments, lambdas, list comps, etc).


Raymond Hettinger
 
P

Paul Rubin

Raymond Hettinger said:
Py2.4 will have a classmethod called list.sorted() that accepts any
iterable as an argument and returns a new sorted list as a result.
It can be used anywhere you can use an expression (function call
arguments, lambdas, list comps, etc).

Oh cool, although it means allocating more memory for the new list.
 
S

Serge Orlov

Bruno Desthuilliers said:
Which is pretty ugly and adds a useless function call.
Why ugly? It adds the functionality you asked for, it doesn't
use any magic, it's readable. I have goodies.py module for
personal stuff like that.
I just can't understand *why* the BDFL made this choice.
The answer is the FAQ section of the Python web site.

-- Serge.
 
K

Kristian Ovaska

John Smith said:
Hi, what's the fastest way (least amount of typing) to sort dictionary
objects by the key?

You could use a custom dictionary that does what you want.

class Mydict(dict):
def sortedkeys(self):
k = self.keys()
k.sort()
return k
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top