[Tutor] sorting dictionary keys?
Kent Johnson
kent37 at tds.net
Tue May 13 14:28:04 CEST 2008
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
More information about the Tutor
mailing list