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

Steven D'Aprano steve at REMOVEMEcyber.com.au
Thu Nov 24 02:27:59 EST 2005


Peter Hansen wrote:

> 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.

As I recall, that guarantee is only if the dict is not 
modified between retrieving the keys and retrieving the 
values.

-- 
Steven.




More information about the Python-list mailing list