Dictionary Views -- good examples? [was Re: Python 3 dict question]

Ethan Furman ethan at stoneleaf.us
Fri May 6 18:49:40 EDT 2011


Ian Kelly wrote:
> On Fri, May 6, 2011 at 1:57 PM, dmitrey <dmitrey15 at gmail.com> wrote:
>> Unfortunately, it doesn't work, it turn out to be dict_items:
>>>>> next({1:2}.items())
>> Traceback (most recent call last):
>>  File "<stdin>", line 1, in <module>
>> TypeError: dict_items object is not an iterator
> 
> So call iter() on it first:
> 
> next(iter(myDict.items()))

Which is becoming less elegant.  Seems to me that View objects should be 
directly iterable, but then I don't really understand the motivation 
behind them or what greatness is facilitated by having them.

Anybody care to chime in with their usage of this construct?

~Ethan~



More information about the Python-list mailing list