[Python-Dev] Repeatability of looping over dicts (original) (raw)
Tim Delaney timothy.c.delaney at gmail.com
Fri Jan 4 21:05:55 CET 2008
- Previous message: [Python-Dev] Repeatability of looping over dicts
- Next message: [Python-Dev] Repeatability of looping over dicts
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
A.M. Kuchling wrote:
So, do Python implementations need to guarantee that list(dictvar) == a later result from list(dictvar)?
As I just posted to the blog, yes. Look at section 3.8 of the reference manual (Mapping Types), specifically footnote 3:
http://docs.python.org/lib/typesmapping.html
(3) 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 pairs. Another way to create the same list is "pairs = [(v, k) for (k, v) in a.iteritems()]".
Tim Delaney
- Previous message: [Python-Dev] Repeatability of looping over dicts
- Next message: [Python-Dev] Repeatability of looping over dicts
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]