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

"Martin v. Löwis" martin at v.loewis.de
Thu Nov 24 23:32:30 CET 2005


Christoph Zwerschke wrote:
> Sorry. Your answer was good; I missed the point and thought you wrote 
> set(d.keys()). Is it documented anywhere that set(d) = set(d.keys())? I 
> think this should go into the Python Doco where keys() are explained.

It follows from what is documented. set(<iterable object>) creates a
set that contains all elements in the iterable object:

http://docs.python.org/lib/built-in-funcs.html#l2h-63

Now, is a dictionary an iterable object? Yes, it is:

http://www.python.org/peps/pep-0234.html

Together, this gives the property I demonstrated.

Unfortunately, the PEP apparently hasn't made it into the
implementation.

Regards,
Martin



More information about the Python-list mailing list