Python 3: dict & dict.keys()

alex23 wuwei23 at gmail.com
Thu Jul 25 02:01:25 EDT 2013


On 25/07/2013 4:31 AM, Ethan Furman wrote:
>    2) Hopefully learn something about when a view is useful.

I haven't seeen this mentioned - forgive me if it's a repeat - but views 
are constant references to whichever set they represent.

Python 2.7:

 >>> dd = dict(a=1,b=2,c=3)
 >>> keys = dd.keys()
 >>> 'a' in keys
True
 >>> dd['d'] = 4
 >>> 'd' in keys
False

Python 3.3:
 >>> dd = dict(a=1,b=2,c=3)
 >>> keys = dd.keys()
 >>> 'a' in keys
True
 >>> dd['d'] = 4
 >>> 'd' in keys
True

If part of my code is only interested in what keys or values are 
present, it doesn't need to be given a reference to the full dictionary, 
just to whichever view it cares about.



More information about the Python-list mailing list