[Python-ideas] Iteritems() function?

Oleg Broytman phd at phdru.name
Wed Jun 8 13:54:49 CEST 2011


On Wed, Jun 08, 2011 at 01:01:08PM +0200, Jan Kaliszewski wrote:
> Quite typical: iterate and do something with some_items -- a collection
> of 2-element items.
>> for first, second in some_items:
> ...
>> But for dicts it must use another form:
>> for first, second in some_items.items():
> ...
>> We must know it'll be a mapping, and even then quite usual bug is to
> forget to add that `items()'.

 You don't need a special buitin for that. Just call .items():
if hasattr(some_items, 'items'):
 some_items = some_items.items()
for first, second in some_items:
 ...
Oleg.
-- 
 Oleg Broytman http://phdru.name/ phd at phdru.name
 Programmers don't die, they just GOSUB without RETURN.


More information about the Python-ideas mailing list

AltStyle によって変換されたページ (->オリジナル) /