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

Christoph Zwerschke cito at online.de
Thu Nov 24 11:31:26 EST 2005


> Martin v. Löwis wrote:

>> If you want the set of keys of a dictionary d, then do set(d):
>>  >>> d={1:2,3:4}
>>  >>> set(d)
>> set([1, 3])
> 
> I know. But this does not answer the question: If the keys *are* already 
> a set according to their semantics, why aren't they returned as a set 
> from the beginning?

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.

I would have expected that set(d) returns set(d.items()), but as has 
been discussed, this would cause problems with mutable values.

-- Christoph



More information about the Python-list mailing list