Why is dictionary.keys() a list and not a set?

Mike Meyer mwm at mired.org
Thu Nov 24 01:00:56 CET 2005


Christoph Zwerschke <cito at online.de> writes:
> Ok, the answer is easy: For historical reasons - built-in sets exist
> only since Python 2.4.
>
> Anyway, I was thinking about whether it would be possible and
> desirable to change the old behavior in future Python versions and let
> dict.keys() and dict.values() both return sets instead of lists.

Two (rhetorical, since you dropped the idea) questions:

Are you sure dict.values() should be a set? After all, values aren't
guaranteed to be unique, so dict(a = 1, b = 1).values() currently
returns [1, 1], but would return set([1]) under your proposal.

What about dict.items()?

> For instance, by allowing the set operator "in" for dictionaries,
> instead of "has_key".

"in" already works for dicdtionaries:

>>> d = dict(a = 1, b = 1)
>>> 'a' in d
True
>>> 'f' in d
False
>>> 

> But could other set methods also be useful?

Looks like it.

      <mike
-- 
Mike Meyer <mwm at mired.org>			http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.



More information about the Python-list mailing list