dict.items() vs dict.iteritems and similar questions

Laurent Pointal laurent.pointal at limsi.fr
Thu Mar 15 05:03:12 EDT 2007


Paul Rubin a écrit :
> Laurent Pointal <laurent.pointal at limsi.fr> writes:
>> Both work, you may prefer xrange/iteritems for iteration on large
>> collections, you may prefer range/items when processing of the result
>> value explicitly need a list (ex. calculate its length) or when you are
>> going to manipulate the original container in the loop.
> 
> You can use len(d) if d is a dict.

Yes, as long as you have a semantic relation between the original dict
and the extracted keys. But once you have extracted the keys, and pass
them around your functions, if you miss the relationship, you have
either a list container or a generator. Not considering the case where
original dict is modified between keys extraction and keys usage...

But as we dont know more about OP needs...


A+

Laurent.



More information about the Python-list mailing list