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

Duncan Booth duncan.booth at invalid.invalid
Thu Nov 24 04:39:59 EST 2005


bonono at gmail.com wrote:

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

It should be fairly clear. 'd.keys()' creates one new object (a list). 
'd.items()' creates a list of tuples, so that is one new list and one 
million new tuples.



More information about the Python-list mailing list