473,401 Members | 2,068 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,401 software developers and data experts.

A little more advanced for loop

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[i], b[i], and c[i]

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

Thanks in advance.

Feb 9 '07 #1
6 4911
Horta wrote:
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[i], b[i], and c[i]

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
Feb 9 '07 #2
Horta wrote:
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[i], b[i], and c[i]

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
Feb 9 '07 #3
On Feb 9, 9:00 am, Stephan Diehl <stephan.di...@gmx.netwrote:
Horta wrote:
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[i], b[i], and c[i]
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!

Feb 9 '07 #4
Horta wrote:
On Feb 9, 9:00 am, Stephan Diehl <stephan.di...@gmx.netwrote:
>Horta wrote:
>> 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[i], b[i], and c[i]
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
Feb 9 '07 #5
Larry Bates <la*********@websafe.comwrote:
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
Feb 10 '07 #6
In article <45**************@websafe.com>,
Larry Bates <la*********@websafe.comwrote:
>
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.
--
Aahz (aa**@pythoncraft.com) <* http://www.pythoncraft.com/

"I disrespectfully agree." --SJM
Feb 10 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Cengiz Ulku | last post by:
Hi, I know now, how to search for a special char. or a word in a RTB control. But, my question is: My RTF file is some sort of an index document, there are chapter numbers and within these...
1
by: steve | last post by:
Hi, I have a 2 demo systems for testing code, they use advance replication. I am seeing single transactions with >6000 calls!!!, obviously something is very wrong. I suspect an...
1
by: nightsaber | last post by:
<script language="JavaScript"> <!-- hide me var the_number = prompt("how many words (3-5 is good)?", "4"); var the_string = ""; var a_word; for (loop = 0; loop < the_number; loop++) {...
8
by: Usman | last post by:
Huy everyone , Well I am not a big C++ programmer , I am just a little young kid on it tryint to learn . Actually I was given an assignment last week by my teacher which I solved ...
3
by: Brian Keating EI9FXB | last post by:
Hello again, I've already placed a few posts on this topic. This time i've a simple application that exhibits my problem, I've placed sample solution 8k on my website should anyone be interested...
5
by: ibe | last post by:
Hi, i can't get the clue: I have had a loop in my code like this: ---snip--- class Foo { accept(Visitor& v) { v.visit(*this); }
0
by: Sharath | last post by:
Quality Globe is Glad to Offer you the Fast Track course on Automation, QTP Basics and Advanced, and Quality Center Starting Date: June 4th, 2007 Timings: 10 AM to 3:30 PM Duration: 50 Hours ...
18
by: miller.paul.w | last post by:
Ruby has a neat little convenience when writing loops where you don't care about the loop index: you just do n.times do { ... some code ... } where n is an integer representing how many times you...
3
by: MattKent | last post by:
Hi there, I'm coming to you nice people as a desperate resort with time working against me, anyway: I have implemented an algorithm that basically generates a very large tree of nodes, where...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.