Will Dict Return Keys and Values in Same Order?
python
Solution
It's hard to improve on the Python documentation:
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(d.values(), d.keys()).` The same relationship holds for the `iterkeys()` and `itervalues()` methods: `pairs = zip(d.itervalues(), d.iterkeys())` provides the same value for pairs. Another way to create the same list is `pairs = [(v, k) for (k, v) in d.iteritems()]`
So, in short, "yes" with the caveat that you must not modify the dictionary in between your call to `keys()` and your call to `values()`.
Problem
Possible Duplicate: Python dictionary: are keys() and values() always the same order? If i have a dictonary in python, will .keys and .values return the corresponding elements in the same order? E.g. ``` foo = {'foobar' : 1, 'foobar2' : 4, 'kittty' : 34743} ``` For the keys it returns: ``` >>> foo.keys() ['foobar2', 'foobar', 'kittty'] ``` Now will foo.values() return the elements always in the same order as their corresponding keys?