Python 3: dict & dict.keys()

Ethan Furman ethan at stoneleaf.us
Thu Jul 25 18:21:02 CEST 2013


On 07/25/2013 09:11 AM, Prasad, Ramit wrote:
> Terry Reedy wrote:
>>
>> On 7/24/2013 4:34 PM, Prasad, Ramit wrote:
>>
>>> I am still not clear on the advantage of views vs. iterators.
>>
>> A1: Views are iterables that can be iterated more than once. Therefore,
>> they can be passed to a function that re-iterates its inputs, or to
>> multiple functions. They support 'x in view' as efficiently as possible.
>> Think about how you would write the non-view equivalent of '(0,None) in
>> somedict.views())'. When set-like, views support some set operations.
>> For .keys, which are always set-like, these operations are easy to
>> implement as dicts are based on a hashed array of keys.
>
> Hmm, that is a change that makes some sense to me. Does the view
> get updated when dictionary changes or is a new view needed? I
> assume the latter.

Nope, the former.  That is a big advantage that the views have over concrete lists: they show the /current/ state, and 
so are always up-do-date.

--
~Ethan~



More information about the Python-list mailing list