keys and values lining up

Tim Peters tim_one at email.msn.com
Mon Sep 27 21:03:24 EDT 1999


[Nathan Clegg]
> If d is a dict, am I guaranteed that d.keys() and d.values() will
> line up, in proper order, if called successively with no modification
> in between?  That is, is the following safe?
>
> k,v = d.keys(), d.values()

Yes, under the mild assumption that you don't have another thread running
that manages to sneak in and mutate d between the calls.  If that's a
potential problem, do

    kv = d.items()  # list of (key, value) tuples

If you want both keys and values, it's clearer to use the method designed
for that purpose anyway.

something-fishy-about-keys-that-don't-unlock-anything-ly y'rs  - tim






More information about the Python-list mailing list