A little more advanced for loop

H

Horta

Hi folks,

Suppose I have to loop over 3 lists being the same size at the same
time and order. How can I do that without using the range() function
or whatever indexing?

Example using range:

a = ['aaa', 'aaaa']
b = ['bb', 'bbbb']
c = ['c', 'cccc']

for i in range(len(a)):
# using a, b, and c

I'm sure there's a elegant way to do that...

Thanks in advance.
 
D

Diez B. Roggisch

Horta said:
Hi folks,

Suppose I have to loop over 3 lists being the same size at the same
time and order. How can I do that without using the range() function
or whatever indexing?

Example using range:

a = ['aaa', 'aaaa']
b = ['bb', 'bbbb']
c = ['c', 'cccc']

for i in range(len(a)):
# using a, b, and c

I'm sure there's a elegant way to do that...



Use zip:

for av, bv, cv in zip(a, b, c):
print av, bv, cv

Diez
 
S

Stephan Diehl

Horta said:
Hi folks,

Suppose I have to loop over 3 lists being the same size at the same
time and order. How can I do that without using the range() function
or whatever indexing?

Example using range:

a = ['aaa', 'aaaa']
b = ['bb', 'bbbb']
c = ['c', 'cccc']

for i in range(len(a)):
# using a, b, and c

I'm sure there's a elegant way to do that...

Thanks in advance.

Sure, there is:

for a_item, b_item , c_item in zip(a,b,c):
# do something
 
H

Horta

Horta said:
Hi folks,
Suppose I have to loop over 3 lists being the same size at the same
time and order. How can I do that without using the range() function
or whatever indexing?
Example using range:
a = ['aaa', 'aaaa']
b = ['bb', 'bbbb']
c = ['c', 'cccc']
for i in range(len(a)):
# using a, b, and c

I'm sure there's a elegant way to do that...
Thanks in advance.

Sure, there is:

for a_item, b_item , c_item in zip(a,b,c):
# do something


Thanks guys!
 
L

Larry Bates

Horta said:
Horta said:
Hi folks,
Suppose I have to loop over 3 lists being the same size at the same
time and order. How can I do that without using the range() function
or whatever indexing?
Example using range:
a = ['aaa', 'aaaa']
b = ['bb', 'bbbb']
c = ['c', 'cccc']
for i in range(len(a)):
# using a, b, and c
I'm sure there's a elegant way to do that...
Thanks in advance.

Sure, there is:

for a_item, b_item , c_item in zip(a,b,c):
# do something


Thanks guys!

Note: if lists are long take a look at itertools izip. zip creates
a list of lists which could take lots of memory/time if they are VERY
large. itertools izip iterates over them in place.

from itertools import izip

for a_item, b_item, c_item in izip(a,b,c):
# do something

-Larry
 
D

Duncan Booth

Larry Bates said:
Note: if lists are long take a look at itertools izip. zip creates
a list of lists which could take lots of memory/time if they are VERY
large. itertools izip iterates over them in place.

That's interesting. I was going to quibble with the assertion that zip
could take lots of time, since on the face of it a loop using izip packs
and unpacks just as many tuples. Fortunately I tried it out before claiming
that zip would be just as fast :)

It would appear that even for short sequences the izip solution is faster.
My guess would be it is because the same tuple objects are being reused in
the izip version.

C:\Python25\Lib>timeit.py -s "a, b, c = [range(1000)]*3" -s "from itertools
import izip" "for p,q,r in izip(a,b,c): pass"
10000 loops, best of 3: 131 usec per loop

C:\Python25\Lib>timeit.py -s "a, b, c = [range(1000)]*3" "for p,q,r in
zip(a,b,c): pass"
1000 loops, best of 3: 212 usec per loop

C:\Python25\Lib>timeit.py -s "a, b, c = [range(100)]*3" -s "from itertools
import izip" "for p,q,r in izip(a,b,c): pass"

100000 loops, best of 3: 13.9 usec per loop

C:\Python25\Lib>timeit.py -s "a, b, c = [range(100)]*3" "for p,q,r in
zip(a,b,c): pass"
10000 loops, best of 3: 22.6 usec per loop

C:\Python25\Lib>timeit.py -s "a, b, c = [range(10)]*3" -s "from itertools
import izip" "for p,q,r in izip(a,b,c): pass"
100000 loops, best of 3: 2.21 usec per loop

C:\Python25\Lib>timeit.py -s "a, b, c = [range(10)]*3" "for p,q,r in
zip(a,b,c): pass"
100000 loops, best of 3: 3.52 usec per loop
 
A

Aahz

Note: if lists are long take a look at itertools izip. zip creates
a list of lists which could take lots of memory/time if they are VERY
large. itertools izip iterates over them in place.

That's half-true -- while izip is faster, it's not enough faster to make
a significant difference unless the lists are "huge" rather than
"large". Remember that the data elements themselves are not copied.
 

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

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,534
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top