[*].items() (was: Re: [Python-Dev] Lockstep iteration - eureka!)

Peter Schneider-Kamp nowonder@nowonder.de
Mon, 14 Aug 2000 04:27:07 +0000


Paul Prescod wrote:
> 
> Just van Rossum wrote:
> >
> >        for <index> indexing <element> in <seq>:
> 
> Let me throw out another idea. What if sequences just had .items()
> methods?
> 
> j=range(0,10)
> 
> for index, element in j.items():

I like the idea and so I've uploaded a patch for this to SF:
https://sourceforge.net/patch/?func=detailpatch&patch_id=101178&group_id=5470

For ease of reading:
This patch adds a .items() method to the list object.
.items() returns a list with of tuples. E.g.:

  for index, value in ["a", "b", "c"].items(): 
      print index, ":", value 

will print: 

  0: a 
  1: b 
  2: c 

I think this is an easy way to achieve looping over
index AND elements in parallel. Semantically the
following two expressions should be equivalent: 

for index, value in zip(range(len(mylist)), mylist):

for index, value in mylist.items():

In opposition to patch #110138 I would call this: 
"Adding syntactic sugar without adding syntax (or sugar<wink>):"

this-doesn't-deserve-new-syntax-ly y'rs
Peter
-- 
Peter Schneider-Kamp          ++47-7388-7331
Herman Krags veg 51-11        mailto:peter@schneider-kamp.de
N-7050 Trondheim              http://schneider-kamp.de