Help with dictionary

Ian Bicking ianb at colorstudy.com
Thu Jun 5 03:22:57 EDT 2003


On Thu, 2003-06-05 at 02:01, Jim Shady wrote:
> I have a dictionary of the sort:
> 
> {(1, (21353, 21418)): 3900, (2, (53006, 53164)): 3800, (0, (19697,
> 19763)): 4100}
> 
> I need to re-sort this dictionary with the first element of the tuple
> in the keys. A sorted dictionary of the above would be:
> 
> {(0, (19697, 19763)): 4100, (1, (21353, 21418)): 3900, (2, (53006,
> 53164)): 3800}

You can't sort dictionaries, you can only sort lists.  So maybe what you
want is:

source = {(1, (21353, 21418)): 3900, (2, (53006, 53164)): 3800, 
          (0, (19697, 19763)): 4100}
dest = source.items()
dest.sort()
print dest
[((0, (19697, 19763)), 4100), ((1, (21353, 21418)), 3900), ((2, (53006,
53164)), 3800)]

Each item of the list is a (key, value) pair.

  Ian







More information about the Python-list mailing list