`Parallel' lists (index and value of list)

Michael Haggerty mhagger at blizzard.harvard.edu
Sat May 15 17:54:24 EDT 1999


> The latter is what Python lacks today, and the source of the frequent
>
>     for i in range(len(seq)):
>         element = seq[i]
>
> idiom.

What if lists had a `values' member like dictionaries do?  Then you
could write

for (i,element) in seq.values():
    print 'Element %d is %s.' % (i, element)

seq.values() would return a list of (index, value) tuples just as it
does for dictionaries (of course for lists they would be in order).
Its implementation would be equivalent to

class UserList:
    ...
    def values(self):
        return map(None, range(len(l)), l)

The advantage is that it requires no new syntax in the language, just
an additional member function for lists which is intuitively analogous
to a member function already present for dictionaries.  It might even
be possible to optimize this construct when used in a for statement.

Just a thought...
Michael

-- 
Michael Haggerty
mhagger at blizzard.harvard.edu




More information about the Python-list mailing list