d.keys() and d.values()


G

Gerrit Holl

Hi,

is it guaranteed that dict(zip(d.keys(), d.values())) == d?
In words, do .keys() and .values() always have the same order? Is
it safe to rely on this?

yours,
Gerrit.
 
Ad

Advertisements

M

Max M

Gerrit said:
> Hi,
>
> is it guaranteed that dict(zip(d.keys(), d.values())) == d?
> In words, do .keys() and .values() always have the same order? Is
> it safe to rely on this?

It says so in the docs.

http://www.python.org/doc/current/lib/typesmapping.html

(3) "Keys and values are listed in random order. If keys() and values()
are called with no intervening modifications to the dictionary, the two
lists will directly correspond. This allows the creation of (value, key)
pairs using zip(): "pairs = zip(a.values(), a.keys())". "


regards Max M
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top