Python 3: dict & dict.keys()
Ethan Furman
ethan at stoneleaf.us
Wed Jul 24 19:22:11 EDT 2013
On 07/24/2013 01:34 PM, Prasad, Ramit wrote:
>
> I am still not clear on the advantage of views vs. iterators. What
> makes d.viewkeys() better than d.iterkeys()? Why did they decide
> not to rename d.iterkeys() to d.keys() and instead use d.viewkeys()?
> Is the iteration over a set operation on keys really that common a
> use case?
From a practical standpoint, iterkeys() is a one-shot deal, while viewkeys() can be iterated over multiple times:
--> d = {1: 'one', 2: 'two', 3: 'three'}
--> di = d.iterkeys()
--> list(di)
[1, 2, 3]
--> list(di)
[]
--> dv = d.viewkeys()
--> list(dv)
[1, 2, 3]
--> list(dv)
[1, 2, 3]
And views are not sets -- they just support a couple set-like operations.
--
~Ethan~
More information about the Python-list
mailing list