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

bonono at gmail.com bonono at gmail.com
Thu Nov 24 04:04:46 EST 2005


Fredrik Lundh wrote:
> bonono at gmail.com wrote:
>
> > I know that is a single list of tuples, I mean that can be used as
> > well.
> >
> > for k, _ in d.items(): print k
> > for _, v in d.items(): print v
> > for k in d.keys(): print k
> > for v in d.values(): print v
> >
> > Would there be a noticeable performance difference ?
>
> Sloppy use of print statements is a great way to produce misleading
> benchmarks [1], since the time it takes to print lots of stuff tends to
> overshadow the actual algorithm (for bonus points, print to a terminal
> window, and use "time" to measure performance, so you get startup
> times as well).  If you don't necessarily want to print all the stuff, my
> original assertion still holds:
>
>     "creating a list full of tuples just to pull them apart and throw
>     them all away isn't exactly the most efficient way to do things"
>
> Consider a dictionary with one million items.  The following operations
>
>     k = d.keys()
>     v = d.values()
>
> creates two list objects, while
>
>     i = d.items()
>
> creates just over one million objects.  In your "equivalent" example,

Sorry. I lose you here. Could you explain in detail what do you mean by
"two list objects" vs one million objects ?




More information about the Python-list mailing list