Iteritems() function?

Case ==== 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()'. But sometimes it may be a dict {first: second, ...} OR a seq [(first, second), ...] and in fact we are not interested in it -- we simply want to iterate over its items... But we are forced to do type/interface check, e.g.: if isinstance(coll, collections.Mapping): for first, second in some_items.items(): ... else: for first, second in some_items: ... Idea ==== A new function: builtins.iteritems() or builtins.iterpairs() or itertools.items() or itertools.pairs() (don't know what name would be the best) -- equivalent to: def <one of the above names>(coll): iterable = (coll.items() if isinstance(coll, collections.Mapping) else coll) return iter(iterable) or maybe something like: def <one of the above names>(coll): try: iterable = coll.items() except AttributeError: iterable = coll return iter(iterable) Usage ===== Then, in our example case, we'd do simply: for first, second in iteritems(some_items): ... And we don't need to think about some_items type, whether it's a mapping or 2-tuple sequence. All we need to know is that it's a collection of 2-element items. Regards, *j

On Wed, Jun 08, 2011 at 01:01:08PM +0200, Jan Kaliszewski wrote:
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@phdru.name Programmers don't die, they just GOSUB without RETURN.

On 06/08/2011 01:54 PM, Oleg Broytman wrote:
So basically it would be: def items(sequence): if hasattr(sequence, 'items'): return sequence.items() else: return sequence I don't think that is enough complexity to justify an inclusion in builtins or itertools. Anyway, I would have expected such a function to do this (so it's not even obvious): def items(sequence): if hasattr(sequence, 'items'): return sequence.items() else: return enumerate(sequence) -panzi

On Thu, Jun 9, 2011 at 1:41 AM, Mathias Panzenböck <grosser.meister.morti@gmx.net> wrote:
I expect the use case here is to implement APIs like the dict constructor and update() method - you can either pass them a mapping, or else an iterable of key-value pairs. As Oleg noted though, the traditional way of handling that is to ducktype on the "items" method, although an isinstance check against Mapping would indeed be an acceptable substitute these days. Either way, calling items() gets you an iterable of key-value pairs, so you write your algorithm to work on that and do a coercion via items() to handle the mapping case. Cheers, Nick. -- Nick Coghlan | ncoghlan@gmail.com | Brisbane, Australia

On Wed, Jun 08, 2011 at 01:01:08PM +0200, Jan Kaliszewski wrote:
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@phdru.name Programmers don't die, they just GOSUB without RETURN.

On 06/08/2011 01:54 PM, Oleg Broytman wrote:
So basically it would be: def items(sequence): if hasattr(sequence, 'items'): return sequence.items() else: return sequence I don't think that is enough complexity to justify an inclusion in builtins or itertools. Anyway, I would have expected such a function to do this (so it's not even obvious): def items(sequence): if hasattr(sequence, 'items'): return sequence.items() else: return enumerate(sequence) -panzi

On Thu, Jun 9, 2011 at 1:41 AM, Mathias Panzenböck <grosser.meister.morti@gmx.net> wrote:
I expect the use case here is to implement APIs like the dict constructor and update() method - you can either pass them a mapping, or else an iterable of key-value pairs. As Oleg noted though, the traditional way of handling that is to ducktype on the "items" method, although an isinstance check against Mapping would indeed be an acceptable substitute these days. Either way, calling items() gets you an iterable of key-value pairs, so you write your algorithm to work on that and do a coercion via items() to handle the mapping case. Cheers, Nick. -- Nick Coghlan | ncoghlan@gmail.com | Brisbane, Australia
participants (5)
-
Jan Kaliszewski
-
Masklinn
-
Mathias Panzenböck
-
Nick Coghlan
-
Oleg Broytman