Marcio Rosa da Silva wrote:

> In other words, it is safe to do:
> 
>  >>> dd = dict(zip(d.values(),d.keys()))

Yes, if the dictionary isn't modified between the invocation of values() and
keys(). Quoting http://docs.python.org/lib/typesmapping.html

"""
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. If items(), keys(), values(), iteritems(),
iterkeys(), and itervalues() are called with no intervening modifications
to the dictionary, the lists will directly correspond. This allows the
creation of (value, key) pairs using zip(): "pairs = zip(a.values(),
a.keys())". The same relationship holds for the iterkeys() and itervalues()
methods: "pairs = zip(a.itervalues(), a.iterkeys())" provides the same
value for 
"""

I suppose you are aware that you lose information if multiple keys have the
same value.

Peter


-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to