Inverse of dict(zip(x,y))

psykeedelik icymist at
Wed Mar 4 11:19:02 CET 2009

On Mar 4, 11:06 am, Paul Rubin <http://phr...@NOSPAM.invalid> wrote:
> lone_eagle <icym... at> writes:
> > So, if x and y are two lists, it is easier to make a dictionary using
> > d = dict(zip(x,y)), but if I have d of the form, d = {x1:y1,
> > x2:y2, ...}, what is there any trick to get lists x = [x1, x2, ...]
> > and y = [y1, y2, ...]
> This may be a bit of a mind bender, but:
>   x, y = zip(*d.items())
> The trick is that if xys is a list of pairs, then zip(*xys) splits
> out the pairs, e.g.:
>      >>> zip(*((1,2),(3,4),(5,6)))
>      [(1, 3, 5), (2, 4, 6)]
> I found that in the python docs somewhere.  The mind wobbles.

That was cool!!

I just checked the python documentation, but the below note worries me
a bit!!

"""Keys and values are listed in an arbitrary order which is non-
random, varies across Python implementations, and depends on the
dictionary’s history of insertions and deletions....."""

I hope it does not mean that the key->value mapping is not guaranteed,
but only that the order of the [key: value] pairs would change. Which
one is right?


More information about the Python-list mailing list