dictionary method question

Tim Peters tim.one at comcast.net
Tue Apr 23 15:51:54 EDT 2002


[dsavitsk]
> 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() or can i count on a
> similarity of the above? in small tests this seems to be the case.

So long as you don't mutate the dict between steps, yes, it's guaranteed
that

    d.keys()   == [k for k, v in d.items()]
and
    d.values() == [v for k, v in d.items()]

In 2.2. it's also guaranteed that

    d.items()  == list(d.iteritems())
    d.keys()   == list(d.iterkeys())
    d.values() == list(d.itervalues())






More information about the Python-list mailing list