Dictionary .keys() and .values() should return a set [with Python3000 in mind]

cmdrrickhunter@yaho.com conrad.ammon at gmail.com
Mon Jul 3 04:45:50 EDT 2006


Yes, this is what he's saying.  Its not "broken," just a bit different.
 After all, you dont have a problem with:
lst = [1, 2, 3]
ptr = lst
lst.append(4) # this changes ptr

And a "view" of the dictionary is orders faster than creating a copy of
it (which is required to keep k0 from changing in your example).  If
you're LUCKY, copying a dictionary is O(n), and i would expect it to
take longer because hashmaps usually have a lot of empty space in them
(anyone with python specific experiance know if its diffent for python
hashmaps?), and each empty space takes time to iterate across.  So a
dictionary is over O(n), while a view takes O(1) time.  considering how
often this operation is used, it makes sense to do this optimization

And if you really want an independant list, you can always say
k0 = list(d.keys())

Paul Rubin wrote:
>
> Wait a sec, you're saying
>
>   k0 = d.keys()
>   d['whee'] = 'parrot'    # this can change k0???
> 
> That sounds broken.




More information about the Python-list mailing list