13 May 14:28
Re: sorting dictionary keys?
From: Kent Johnson <kent37 <at> tds.net>
Subject: Re: sorting dictionary keys?
Newsgroups: gmane.comp.python.tutor
Date: 2008-05-13 12:28:04 GMT
Subject: Re: sorting dictionary keys?
Newsgroups: gmane.comp.python.tutor
Date: 2008-05-13 12:28:04 GMT
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
RSS Feed