Connecting Tech Pros Worldwide Forums | Help | Site Map

sorting dictionary keys?

John Smith
Guest
 
Posts: n/a
#1: Jul 18 '05
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.



Jay O'Connor
Guest
 
Posts: n/a
#2: Jul 18 '05

re: sorting dictionary keys?


John Smith wrote:
[color=blue]
> Hi, what's the fastest way (least amount of typing)[/color]

Never good criteria
[color=blue]
>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.
>[/color]

sort() sorts in place and returns None

Emile van Sebille
Guest
 
Posts: n/a
#3: Jul 18 '05

re: sorting dictionary keys?


John Smith[color=blue]
> Hi, what's the fastest way (least amount of typing) to sort[/color]
dictionary[color=blue]
> objects by the key? What's the fastest way of iterating over sorted[/color]
keys[color=blue]
> 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():[/color]

sort sorts in place and doesn't return anything. In 2.4 you'll have
the sorted method.
[color=blue]
> print k, d[k]
>
> why doesn't the nested function call work? Thanks in advance.
>
>[/color]

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

--

Emile van Sebille
emile@fenx..com

Bruno Desthuilliers
Guest
 
Posts: n/a
#4: Jul 18 '05

re: sorting dictionary keys?


Jay O'Connor wrote:[color=blue]
> John Smith wrote:
>[color=green]
> > Hi, what's the fastest way (least amount of typing)[/color]
>
> Never good criteria
>[color=green]
>> 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.
>>[/color]
>
> sort() sorts in place and returns None
>[/color]

<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

Serge Orlov
Guest
 
Posts: n/a
#5: Jul 18 '05

re: sorting dictionary keys?


[color=blue]
> <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>[/color]

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

-- Serge


Jay O'Connor
Guest
 
Posts: n/a
#6: Jul 18 '05

re: sorting dictionary keys?


Bruno Desthuilliers wrote:
[color=blue]
> Jay O'Connor wrote:
>[color=green]
>> John Smith wrote:
>>[color=darkred]
>> > Hi, what's the fastest way (least amount of typing)[/color]
>>
>> Never good criteria
>>[color=darkred]
>>> 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.
>>>[/color]
>>
>> sort() sorts in place and returns None
>>[/color]
>
> <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>[/color]


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()

Bruno Desthuilliers
Guest
 
Posts: n/a
#7: Jul 18 '05

re: sorting dictionary keys?


Serge Orlov wrote:[color=blue][color=green]
>><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>[/color]
>
>
> <hint>
> def sort(lst):
> lst.sort()
> return lst
> </hint>[/color]

Which is pretty ugly and adds a useless function call.

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

Peter Hansen
Guest
 
Posts: n/a
#8: Jul 18 '05

re: sorting dictionary keys?


Bruno Desthuilliers wrote:[color=blue]
>
> Serge Orlov wrote:[color=green][color=darkred]
> >><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>[/color]
> >
> >
> > <hint>
> > def sort(lst):
> > lst.sort()
> > return lst
> > </hint>[/color]
>
> Which is pretty ugly and adds a useless function call.
>
> I just can't understand *why* the BDFL made this choice.[/color]

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

-Peter
Raymond Hettinger
Guest
 
Posts: n/a
#9: Jul 18 '05

re: sorting dictionary keys?


> > sort() sorts in place and returns None[color=blue][color=green]
> >[/color]
>
> <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[/color]


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


Paul Rubin
Guest
 
Posts: n/a
#10: Jul 18 '05

re: sorting dictionary keys?


"Raymond Hettinger" <vze4rx4y@verizon.net> writes:[color=blue]
> 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).[/color]

Oh cool, although it means allocating more memory for the new list.
Serge Orlov
Guest
 
Posts: n/a
#11: Jul 18 '05

re: sorting dictionary keys?



"Bruno Desthuilliers" <bdesth.nospam@removeme.free.fr> wrote in message news:3fc935da$0$28643$636a55ce@news.free.fr...[color=blue]
> Serge Orlov wrote:[color=green][color=darkred]
> >><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>[/color]
> >
> >
> > <hint>
> > def sort(lst):
> > lst.sort()
> > return lst
> > </hint>[/color]
>
> Which is pretty ugly and adds a useless function call.[/color]
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.
[color=blue]
>
> I just can't understand *why* the BDFL made this choice.[/color]
The answer is the FAQ section of the Python web site.

-- Serge.


Kristian Ovaska
Guest
 
Posts: n/a
#12: Jul 18 '05

re: sorting dictionary keys?


"John Smith" <asdf@asdf.com>:[color=blue]
>Hi, what's the fastest way (least amount of typing) to sort dictionary
>objects by the key?[/color]

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

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

--
Kristian Ovaska - http://www.cs.helsinki.fi/u/hkovaska/
Closed Thread