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

Peter Hansen peter at engcorp.com
Wed Nov 23 20:52:04 EST 2005


Christoph Zwerschke wrote:
> 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.

Definitely not.  I believe it's currently guaranteed that the order of 
the items in dict.keys() and dict.values() will match (i.e. the index of 
any key in its list will be the same as the index of the corresponding 
value in its list).  This property is almost certainly used in some 
code, so it can't be broken without good reason.

-Peter




More information about the Python-list mailing list