[Tutor] dict.iteritems() in Python3, was Re: Tutor Digest, Vol 110, Issue 117
Peter Otten
__peter__ at web.de
Tue Apr 30 19:44:28 CEST 2013
Prasad, Ramit wrote:
> Jim Mooney wrote:
>>
>> > In py3.x, iteritems was replaced by .items()
>>
>> Interesting, since iteritems was in my book, which was "updated" for
>> Py33. I guess the moral is you shouldn't trust an author 100% ;') I
>> must admit, iteritems did seem awkward and annoying so I'm glad it's
>> dropped.
>>
>> Jim
>
> Technically, .items() was dropped and .iteritems() was renamed .items()
One way to verify that is to run
$ cat tmp.py
d = dict(a=1, b=2)
x = d.items()
y = d.iteritems()
z = d.viewitems()
through the 2to3 tool:
$ 2to3 tmp.py 2> /dev/null
--- tmp.py (original)
+++ tmp.py (refactored)
@@ -1,5 +1,5 @@
d = dict(a=1, b=2)
-x = d.items()
-y = d.iteritems()
-z = d.viewitems()
+x = list(d.items())
+y = iter(d.items())
+z = d.items()
So
d.items() --> list(d.items())
d.iteritems() --> iter(d.items())
d.viewitems() --> d.items()
In short: items() and iteritems() were both dropped, and viewitems() was
renamed items().
More information about the Tutor
mailing list