Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #51175

Re: Python 3: dict & dict.keys()

Date 2013-07-24 16:22 -0700
From Ethan Furman <ethan@stoneleaf.us>
Subject Re: Python 3: dict & dict.keys()
References (1 earlier) <ksnrr9$k4t$1@ger.gmane.org> <ksp2it$21p$1@ger.gmane.org> <51F01D9E.2030903@stoneleaf.us> <kspbms$atn$1@ger.gmane.org> <5B80DD153D7D744689F57F4FB69AF474185C131B@SCACMX008.exchad.jpmchase.net>
Newsgroups comp.lang.python
Message-ID <mailman.5072.1374709915.3114.python-list@python.org> (permalink)

Show all headers | View raw


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~

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: Python 3: dict & dict.keys() Ethan Furman <ethan@stoneleaf.us> - 2013-07-24 16:22 -0700

csiph-web