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

bonono at gmail.com bonono at gmail.com
Thu Nov 24 12:27:43 CET 2005

Fredrik Lundh wrote:
> bonono at gmail.com wrote:
> > These results make more sense. However, I am still puzzled :
> >
> > 1. why would d.keys()/d.values() only return one list element ? Why
> > isn't it a list of 1M element of either the keys or values but items()
> > is ?
> "keys" returns a single list object which contains references to existing
> key objects.  "items" has to create one million new pair objects (which
> in turn hold references to existing key and value objects).
Ah, that is clearer now. So keys()/items() create a list of 1M
reference(object identity?) to existing objects, items() also contains
1 M reference, but to a newly created 1M tuples.

How about the other puzzle of whether seperate keys()/values() pair
have real performance gain in real use ?

More information about the Python-list mailing list