dictionary method question

Max M maxm at mxm.dk
Tue Apr 23 16:15:36 EDT 2002


dsavitsk wrote:


> for some dictionary d is there any relationship between the returns from
> d.keys() and d.values()?  that is, to get them in some arbitrary but
> matching order, do i need to loop over d.items()

Actually it is guaranteed in the docs that if you call keys() and 
values(), without modifying the dict in-between, that they will have the 
same/corresponding order.

So:

keys = aDict.keys()
values = aDict.values()
items = [(keys[i], values[i]) in range(len(keys))]


is the same as:

items = aDict.items()

regards Max M




More information about the Python-list mailing list