Victor Bouffier wrote: > Hi Alan and Ken, > > I think know the difference between using items() vs. iteritems() and > their equivalent for keys and values. I notice Ken suggests iteritems(), > while Alan suggests items() only. > > Does one approach have an advantage over the other? > Should we use only one of them favorably?
It doesn't make much difference for small dictionaries. keys(), values() and items() create new lists with the specified elements. iterkeys(), itervalues() and iteritems() create iterators that return the specified elements in sequence. So for the common case of iterating over dict elements with a for loop, the 'iter' variants conserve memory and may be faster (but always check!) because of the cost of creating the list. The iter variants are relatively new (since Python 2.2). I used to use the older variants in examples here so I wouldn't have to explain the difference :-) but ISTM that modern usage is heading to prefer the iter versions and I am starting to use them myself. But I guess you will not notice any difference in performance until you have dicts with many thousands of elements. Kent _______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor