any such thing as list interleaving?

T

Tom Plunket

I find myself often doing the following sort of thing (sorry for
lack of whitespace, I don't want the line to break):

for entry, index in map(lambda e,i:(e,i),aList,range(len(aList)):
# ...

This definitely seems like a roundabout way to loop through
parallel lists together. Is this map routine truly the easiest/
best/most straight-forward way to do a for loop through parallel
lists, if I feel that the Python anti-idom of:

for index in range(len(myList)):
entry = aList(index)
anotherEntry = anotherList(index)
# ...

???

This also brings up a similar problem for me when iterating over
dictionaries:

for key in myDict:
value = myDict[key]
# ...

This seems a pretty sloppy way to go about it, imo. There must
be something more in the Python spirit! :)

Thanks.

-tom!
 
?

=?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?=

Tom said:
I find myself often doing the following sort of thing (sorry for
lack of whitespace, I don't want the line to break):

for entry, index in map(lambda e,i:(e,i),aList,range(len(aList)):
# ...

In Python 2.3, you can write

for index, entry in enumerate(L):
# ...

For 2.2, you can define enumerate yourself:

def enumerate(L):
i = 0
while 1:
try:
yield i, L
except IndexError:
return
i += 1

For older versions, yet another definition would be needed;
I leave that as an exercise.
This also brings up a similar problem for me when iterating over
dictionaries:

for key in myDict:
value = myDict[key]
# ...

This seems a pretty sloppy way to go about it, imo. There must
be something more in the Python spirit! :)

Here, you could always write

for key, value in myDict.items():
#...

Since 2.2, there is another method available which does not create
a list of tuples:

for key, value in myDict.iteritems():
#...

HTH,
Martin
 
G

Gonçalo Rodrigues

I find myself often doing the following sort of thing (sorry for
lack of whitespace, I don't want the line to break):

for entry, index in map(lambda e,i:(e,i),aList,range(len(aList)):
# ...

This definitely seems like a roundabout way to loop through
parallel lists together. Is this map routine truly the easiest/
best/most straight-forward way to do a for loop through parallel
lists, if I feel that the Python anti-idom of:

Check the zip builtin:
Help on built-in function zip:

zip(...)
zip(seq1 [, seq2 [...]]) -> [(seq1[0], seq2[0] ...), (...)]

Return a list of tuples, where each tuple contains the i-th
element
from each of the argument sequences. The returned list is
truncated
in length to the length of the shortest argument sequence.

for index in range(len(myList)):
entry = aList(index)
anotherEntry = anotherList(index)
# ...

???

This also brings up a similar problem for me when iterating over
dictionaries:

for key in myDict:
value = myDict[key]
# ...

Fire the interpreter and type:

As it's a long stretch of text, I'll just post the relevant part:

| iteritems(...)
| D.iteritems() -> an iterator over the (key, value) items of D
|
This seems a pretty sloppy way to go about it, imo. There must
be something more in the Python spirit! :)

Thanks.

-tom!

With my best regards,
G. Rodrigues
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top