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

Delaney, Timothy (Tim) tdelaney at avaya.com
Sun Jul 2 19:56:09 EDT 2006


vatamane at gmail.com wrote:

> This has been bothering me for a while. Just want to find out if it
> just me or perhaps others have thought of this too: Why shouldn't the
> keyset of a dictionary be represented as a set instead of a list?

There has been much discussion of this on the Python-3000 mailing list.
Suggestings included making keys(), etc return an iterator, and getting
rid of iterkeys(), etc.

The eventual consensus was that keys(), etc should return views on the
dictionary. These views would be re-iterable and will have basically the
same behaviour as the lists returned from keys(), etc. However, such a
view could have O(1) __contains__ behaviour, and would not incur the
overhead of creating a list and populating it - they would be backed by
the dictionary. Of course, this introduces the issue of concurrent
modification, but you can always get an independent copy by calling
list(dict.keys()).

Tim Delaney



More information about the Python-list mailing list