tags:

views:

807

answers:

5

It looks like the lists returned by keys() and values() methods of a dictionary are always a 1-to-1 mapping (assuming the dictionary is not altered between calling the 2 methods).

For example:

>>> d = {'one':1, 'two': 2, 'three': 3}
>>> k, v = d.keys(), d.values()
>>> for i in range(len(k)):
    print d[k[i]] == v[i]

True
True
True

If you do not alter the dictionary between calling keys() and calling values(), is it wrong to assume the above for-loop will always print True? I could not find any documentation confirming this.

+12  A: 

Yes, what you observed is indeed a guaranteed property -- keys(), values() and items() return lists in congruent order if the dict is not altered. iterkeys() &c also iterate in the same order as the corresponding lists.

Alex Martelli
-1: no reference to the documentation (or source).
S.Lott
Dude, that's Alex Martelli, he's the author of Python in a Nutshell and The Python Cookbook. He doesn't need provide a reference.
apphacker
@apphacker: Could be anyone using his name.
nosklo
@apphacker: Even if it was GvR, the BDFL, I'd still ask for a reference.
S.Lott
@S.Lott: I'll ask Guido tomorrow, as he shows up to give his keynote at Pycon Italia, what he thinks about your strong thesis -- i.e. that it's better to give no answer at all, rather than give an authoritative answer without a doc pointer, when one has time to give the answer but no time to google search in the docs for it. As he's a pragmatist, I suspect he'll agree with me -- that a precise answer is better than no answer, and providing "chapter and verse" is only important in religious contexts, not in pragmatical ones;-)
Alex Martelli
@nosklo: yep, sure, I could be anybody -- pure coincidence that Joel showed up at the 'plex for a talk, I had lunch with him, and exactly the next day, 12 days ago, I showed up here for the first time...!-)
Alex Martelli
@Alex Martelli: when there's money on the line (i.e., my project depends on the answer) I like to ask for references. In order to maintain the value of SO, I like to ask for references. While I see your point on "no time to google search"; answers can be edited, and anyone could have inserted the reference (even me). To make SO authoritative (per Joel S.), I think references are valuable.
S.Lott
@Alex Martelli: Sorry, but I have no means of knowing if that is true. Everybody can have lunch with Joel anytime, that doesn't make his identity in Stackoverflow proven.
nosklo
@S.Lott: excellent point, thanks. @nosklo: true, so I added a pointer to http://stackoverflow.com/users/95810/alex-martelli to my Google profile http://www.google.com/profiles/aleaxit -- "proven" enough for you now?-)
Alex Martelli
+2  A: 

According to http://docs.python.org/dev/py3k/library/stdtypes.html#dictionary-view-objects , the keys(), values() and items() methods of a dict will return corresponding iterators whose orders correspond. However, I am unable to find a reference to the official documentation for python 2.x for the same thing.

So as far as I can tell, the answer is yes, but only in python 3.0+

sykora
guaranteed in python 2, see below.
apphacker
Answers shift, directions don't work
Casebash
+8  A: 

Yes it is guaranteed in python 2.x:

If items(), keys(), values(), iteritems(), iterkeys(), and itervalues() are called with no intervening modifications to the dictionary, the lists will directly correspond.

apphacker
Your answer is either wrong, or your link is wrong, because you've given exactly the same link as I have, and that isn't for 2.x
sykora
Ah, haha, copy paste failure. :(
apphacker
your link is still wrong. Do you want me to edit it for you?
nosklo
+2  A: 

For what it's worth, some heavy used production code I have written is based on this assumption and I never had a problem with it. I know that doesn't make it true though :-)

If you don't want to take the risk I would use iteritems() if you can.

for key, value in myDictionary.iteritems():
    print key, value
Koen Bok
A: 

Found this:

If items(), keys(), values(), iteritems(), iterkeys(), and itervalues() are called with no intervening modifications to the dictionary, the lists will directly correspond.

On 2.x documentation.

nosklo
Perfect, thanks for the link
jcoon
What? How is that different from what I posted? He just copied me.
apphacker
Not sure why I got negative votes. My answer is correct and has a link to the correct documentation.
nosklo
I suspect you've been downvoted, nosklo, because your answer is almost word-for-word apphacker's, with the exception of the link. If you answered 1 hour after him, many folks will feel that you couldn't have just "found this", but that you saw his answer, and rather than point out he had the wrong link, you decided to post your own. The information apphacker quoted in his answer is correctly at the link, he just mislabeled it as py2.x... in this case, the equivalent of a harmless typo when the word is clearly recognizable. Your answer may seem avaricious in that context.
Jarret Hardie
@Jarret Hardie: Well, questions are filled of duplicated correct answers. I've never downvoted any answer if it is correct. I just don't upvote them.
nosklo
@Jarret Hardie: And who did that you said is apphacker, he was the one that posted a new answer instead of editing sykora's. He even left a comment.
nosklo
@Jarret Hardie: does that mean that copies are now considered a no-no and are voted down? Joel's blog post introducing SO even suggested copying and improving and collecting those rep points at the end of the rainbow. In any case, I agree with nosklo: I won't downvote a correct answer, even if it's a copy.
ΤΖΩΤΖΙΟΥ
If you are going to copy from another answer, you need to be providing significant new content - otherwise you should use a comment
Casebash