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

bonono at gmail.com bonono at gmail.com
Wed Nov 23 22:19:40 EST 2005


Peter Hansen wrote:
> 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.

Interesting, but why it guaranteed this as functionality wise they are
two different things and since dict is unordered, there is no need for
such guarantee, would it be just implementation consequence ?

This to me is even more uncertain than the zip(it,it) case.




More information about the Python-list mailing list