keys and values lining up

Fred L. Drake, Jr. fdrake at acm.org
Mon Sep 27 22:06:01 EDT 1999


Nathan Clegg writes:
 > 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()

  As someone pointed out, the implementation makes this happen modulo
thread interference.  I asked Guido about this a couple of years ago,
and he said he was willing to make that guarantee.
  Others have pointed out the .items() method; if that makes more
sense for your application, it is better because it avoids the
potentional for thread interference.  However, sometimes using the
individual methods is easier for the surrounding code if you really
want to separate lists, or if you want to end up with (value, key)
pairs (I think that's why I'd been interested in the question when I
asked Guido).


  -Fred

--
Fred L. Drake, Jr.	     <fdrake at acm.org>
Corporation for National Research Initiatives




More information about the Python-list mailing list