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

bonono at gmail.com bonono at gmail.com
Thu Nov 24 03:20:26 EST 2005


Fredrik Lundh wrote:
> bonono at gmail.com wrote:
>
> > Fredrik Lundh wrote:
> > > performance is of course another aspect; if you *need* two parallel
> > > lists, 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.
> > >
> > > (if performance didn't matter at all, we could get rid most dictionary
> > > methods; "iterkeys", "in", and locking should be enough, right?)
>
> > If I need two parallel list(one key, one value), I can still use  the
> > same [(k,v)] tuple, just access it as x[0], x[1].
>
> that's a single list containing tuples, not two parallel lists.
>
> this is two parallel lists:
>
>     k = d.keys()
>     v = d.values()
>     assert isinstance(k, list)
>     assert isinstance(v, list)
>     use(k, v)
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 ?




More information about the Python-list mailing list