James Hartley | 13 May 13:05
Picon

sorting dictionary keys?

I suspect this is a brain-dead question...

Given the following code, output is as expected:

$ cat test.py
d = { 'a' : 1, 'd' : 2, 'b' : 3, 'c' : 0 }

for i in d.keys():
    print "%s\t%s" % (i, d[i])
$ python test.py
a       1
c       0
b       3
d       2
$

But if the keys are sorted, I get an error:
$ cat test1.py
d = { 'a' : 1, 'd' : 2, 'b' : 3, 'c' : 0 }

for i in d.keys().sort():
    print "%s\t%s" % (i, d[i])
$ python test1.py
Traceback (most recent call last):
  File "test.py", line 3, in <module>
    for i in d.keys().sort():
TypeError: 'NoneType' object is not iterable
$

What is the correct manner to iterate through sorted dictionary keys?
(Continue reading)

Mugund K | 13 May 13:32
Picon
Favicon

Re: sorting dictionary keys?

A quick but ugly [brain-dead :-) ]fix would be sorting keys seperately,
 
>>> for i in d.keys():
 print "%s\t%s" % (i, d[i])
 
 
a 1
c 0
b 3
d 2
>>> temp = d.keys()
>>> temp.sort()
>>> for i in temp:
 print "%s\t%s" % (i, d[i])
 
 
a 1
b 3
c 0
d 2
>>>
 
Thnx,
Mugund

--- On Tue, 5/13/08, James Hartley <jjhartley <at> gmail.com> wrote:
From: James Hartley <jjhartley <at> gmail.com>
Subject: [Tutor] sorting dictionary keys?
To: tutor <at> python.org
Date: Tuesday, May 13, 2008, 11:06 AM

I suspect this is a brain-dead question... Given the following code, output is as expected: $ cat test.py d = { 'a' : 1, 'd' : 2, 'b' : 3, 'c' : 0 } for i in d.keys(): print "%s\t%s" % (i, d[i]) $ python test.py a 1 c 0 b 3 d 2 $ But if the keys are sorted, I get an error: $ cat test1.py d = { 'a' : 1, 'd' : 2, 'b' : 3, 'c' : 0 } for i in d.keys().sort(): print "%s\t%s" % (i, d[i]) $ python test1.py Traceback (most recent call last): File "test.py", line 3, in <module> for i in d.keys().sort(): TypeError: 'NoneType' object is not iterable $ What is the correct manner to iterate through sorted dictionary keys? Thanks. Jim _______________________________________________ Tutor maillist - Tutor <at> python.org http://mail.python.org/mailman/listinfo/tutor

Be a better friend, newshound, and know-it-all with Yahoo! Mobile. Try it now.
_______________________________________________
Tutor maillist  -  Tutor <at> python.org
http://mail.python.org/mailman/listinfo/tutor
Kent Johnson | 13 May 14:25
Gravatar

Re: sorting dictionary keys?

On Tue, May 13, 2008 at 7:32 AM, Mugund K <k.mugund <at> yahoo.com> wrote:
>
> A quick but ugly [brain-dead :-) ]fix would be sorting keys seperately,

> >>> temp = d.keys()
> >>> temp.sort()
> >>> for i in temp:

Not so ugly; before sorted() was introduced (Python 2.4) that would be
the way to do it.

Kent
_______________________________________________
Tutor maillist  -  Tutor <at> python.org
http://mail.python.org/mailman/listinfo/tutor

Kent Johnson | 13 May 13:40
Gravatar

Re: sorting dictionary keys?

On Tue, May 13, 2008 at 7:06 AM, James Hartley <jjhartley <at> gmail.com> wrote:

>  But if the keys are sorted, I get an error:
>  $ cat test1.py
>  d = { 'a' : 1, 'd' : 2, 'b' : 3, 'c' : 0 }
>
>  for i in d.keys().sort():
>     print "%s\t%s" % (i, d[i])
>  $ python test1.py
>  Traceback (most recent call last):
>   File "test.py", line 3, in <module>
>     for i in d.keys().sort():

  for i in sorted(d.keys()):
or simply
  for i in sorted(d):
since iterating a dict gives its keys.

The problem is that the inplace sort() returns None.
d.keys() is a (new) list containing the keys
d.keys().sort() gets the list of keys and sorts it, but the value of
the expression is None, so you are essentially writing
  for i in None:
which gives the TypeError you see.

The builtin function sorted() takes any iterable as an argument and
*returns* a sorted sequence so you can use it in an expression.

Kent
_______________________________________________
Tutor maillist  -  Tutor <at> python.org
http://mail.python.org/mailman/listinfo/tutor

Norman Khine | 13 May 13:58
Favicon

Re: sorting dictionary keys?

how about this

 >>> d = { 'a' : 1, 'd' : 2, 'b' : 3, 'c' : 0 }
 >>> for i in sorted(set(d)):
...     print "%s\t%s" % (i, d[i])
...
a       1
b       3
c       0
d       2

James Hartley wrote:
> I suspect this is a brain-dead question...
> 
> Given the following code, output is as expected:
> 
> $ cat test.py
> d = { 'a' : 1, 'd' : 2, 'b' : 3, 'c' : 0 }
> 
> for i in d.keys():
>     print "%s\t%s" % (i, d[i])
> $ python test.py
> a       1
> c       0
> b       3
> d       2
> $
> 
> But if the keys are sorted, I get an error:
> $ cat test1.py
> d = { 'a' : 1, 'd' : 2, 'b' : 3, 'c' : 0 }
> 
> for i in d.keys().sort():
>     print "%s\t%s" % (i, d[i])
> $ python test1.py
> Traceback (most recent call last):
>   File "test.py", line 3, in <module>
>     for i in d.keys().sort():
> TypeError: 'NoneType' object is not iterable
> $
> 
> What is the correct manner to iterate through sorted dictionary keys?
> 
> Thanks.
> 
> Jim
> _______________________________________________
> Tutor maillist  -  Tutor <at> python.org
> http://mail.python.org/mailman/listinfo/tutor
> 
_______________________________________________
Tutor maillist  -  Tutor <at> python.org
http://mail.python.org/mailman/listinfo/tutor

Kent Johnson | 13 May 14:28
Gravatar

Re: sorting dictionary keys?

On Tue, May 13, 2008 at 7:58 AM, Norman Khine <norman <at> khine.net> wrote:
> how about this
>
>
>  >>> d = { 'a' : 1, 'd' : 2, 'b' : 3, 'c' : 0 }
>  >>> for i in sorted(set(d)):
>  ...     print "%s\t%s" % (i, d[i])

The set() is not needed.

Also to iterate over key, value pairs in order by key you can use this:
for k, v in sorted(d.items()):
  print '%s\t%s' (k, v)

Kent
_______________________________________________
Tutor maillist  -  Tutor <at> python.org
http://mail.python.org/mailman/listinfo/tutor


Gmane