Why does the default dictionary iterator in Python iterate through dictionary keys instead of (key, value) tuples?
-
Is there any reasoning behind this except the compatibility with original dictionary API?
-
Answer:
Quoting from the http://legacy.python.org/dev/peps/pep-0234/, There has been a long discussion about whether for x in dict: should assign x the successive keys, values, or items of the dictionary. The symmetry between "if x in y" and "for x in y" suggests that it should iterate over keys. This symmetry has been observed by many independently and has even been used to "explain" one using the other. This is because for sequences, "if x in y" iterates over y comparing the iterated values to x. If we adopt both of the above proposals, this will also hold for dictionaries. The argument against making "for x in dict" iterate over the keys comes mostly from a practicality point of view: scans of the standard library show that there are about as many uses of "for x in dict.items()" as there are of "for x in dict.keys()", with the items() version having a small majority. Presumably many of the loops using keys() use the corresponding value anyway, by writing dict[x], so (the argument goes) by making both the key and value available, we could support the largest number of cases. While this is true, I (Guido) find the correspondence between "for x in dict" and "if x in dict" too compelling to break, and there's not much overhead in having to write dict[x] to explicitly get the value. For fast iteration over items, use "for key, value in dict.iteritems()". I've timed the difference between for key in dict: dict[key] and for key, value in dict.iteritems(): pass and found that the latter is only about 7% faster. Resolution: By BDFL pronouncement, for x in dict iterates over the keys, and dictionaries have iteritems(), iterkeys(), and itervalues() to return the different flavors of dictionary iterators.
Sakthipriyan Vairamani at Quora Visit the source
Related Q & A:
- Why did Russia default on its domestic bonds in 1998 when it could simply print money?Best solution by Quora
- How can I implement an atomic incr and decr on top of an eventually consistent key-value store?Best solution by Stack Overflow
- How to delete an entry from a dictionary in Python?Best solution by Stack Overflow
- What is the best key-value database?Best solution by Stack Overflow
- What is the biggest key in the Florida Keys?Best solution by wiki.answers.com
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.