[Python-checkins] CVS: python/nondist/peps pep-0234.txt,1.12,1.13
Guido van Rossum
gvanrossum@users.sourceforge.net
Tue, 01 May 2001 05:15:44 -0700
Update of /cvsroot/python/python/nondist/peps
In directory usw-pr-cvs1:/tmp/cvs-serv6868
Modified Files:
pep-0234.txt
Log Message:
I've implemented iterkeys(), itervalues() and iteritems() methods for
dictionaries. These do away with the myth that iterating over the
keys and extracting the values is as fast as iterating over the items;
it is about 7% slower.
Index: pep-0234.txt
===================================================================
RCS file: /cvsroot/python/python/nondist/peps/pep-0234.txt,v
retrieving revision 1.12
retrieving revision 1.13
diff -C2 -r1.12 -r1.13
*** pep-0234.txt 2001/05/01 11:47:29 1.12
--- pep-0234.txt 2001/05/01 12:15:42 1.13
***************
*** 167,170 ****
--- 167,182 ----
(either by the loop or by another thread) are not violated.
+ - Add methods to dictionaries that return different kinds of
+ iterators explicitly:
+
+ for key in dict.iterkeys(): ...
+
+ for value in dict.itervalues(): ...
+
+ for key, value in dict.iteritems(): ...
+
+ This means that "for x in dict" is shorthand for "for x in
+ dict.iterkeys()".
+
If this proposal is accepted, it makes sense to recommend that
other mappings, if they support iterators at all, should also
***************
*** 300,321 ****
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. I've also timed the difference between
for key in dict: dict[key]
and
-
- for key, value in dict[key]: pass
-
- and found that these run at about the same speed.
-
- We could also add methods to dictionaries that return different
- kinds of iterators, e.g.
! for key, value in dict.iteritems(): ...
!
! for value in dict.itervalues(): ...
! for key in dict.iterkeys(): ...
--- 312,327 ----
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.