473,581 Members | 2,424 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Loop three lists at the same time?

Hi all,

I have three lists with the same length. Is there any method to loop
the three lists without a loop counter?

Best regards,
Davy

Nov 13 '07 #1
5 1130
Davy wrote:
Hi all,

I have three lists with the same length. Is there any method to loop
the three lists without a loop counter?

Best regards,
Davy
Hello,

the zip function?
>>list1 = [1,2,3]
list2 = [4,5,6]
list3 = [7,8,9]
for a,b,c in zip(list1,list2 ,list3):
.... print a, b, c
....
1 4 7
2 5 8
3 6 9

hth
j.
Nov 13 '07 #2
On Nov 13, 11:46 am, Davy <zhushe...@gmai l.comwrote:
Hi all,

I have three lists with the same length. Is there any method to loop
the three lists without a loop counter?

Best regards,
Davy
What exactly do you mean?

Are you trying to loop them together with the same ´count´. "for loop
in zip(1,2,3) kind of thing or something else.

Please either post a more descriptive question, or your current code.

Coko

Nov 13 '07 #3
En Tue, 13 Nov 2007 07:46:09 -0300, Davy <zh*******@gmai l.comescribió:
I have three lists with the same length. Is there any method to loop
the three lists without a loop counter?
Try zip or itertools.izip:

pyL1 = ['a','b','c']
pyL2 = [1, 2, 3]
pyL3 = ['I', 'II', 'III']
pyfrom itertools import izip
pyfor x,y,z in izip(L1,L2,L3):
.... print x,y,z
....
a 1 I
b 2 II
c 3 III

--
Gabriel Genellina

Nov 13 '07 #4
On 11 13 , 6 46 , Davy <zhushe...@gmai l.comwrote:
Hi all,

I have three lists with the same length. Is there any method to loop
the three lists without a loop counter?

Best regards,
Davy
Maybe you just need this:-)
>>list1 = [1, 2, 3]
list2 = [4, 5, 6]
list3 = [7, 8, 9]
for i in list1+list2+lis t3:
.... print i
....

Nov 14 '07 #5
In article <11************ **********@v23g 2000prn.googleg roups.com>,
Davy <zh*******@gmai l.comwrote:
Hi all,

I have three lists with the same length. Is there any method to loop
the three lists without a loop counter?

Best regards,
Davy
Davy,

Look at the "zip" function,

<http://docs.python.org/lib/built-in-funcs.html>

Look near the bottom of the page.

Example:
zip([1,2,3], [4,5,6], [7,8,9])
==[(1, 4, 7), (2, 5, 8), (3, 6, 9)]

Cheers,
-M

--
Michael J. Fromberger | Lecturer, Dept. of Computer Science
http://www.dartmouth.edu/~sting/ | Dartmouth College, Hanover, NH, USA
Nov 14 '07 #6

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

Similar topics

5
2727
by: flupke | last post by:
Hi, i'm having trouble with deleting elements from a list in a for loop ============== test program ============== el = print "**** Start ****" print "List = %s " % el index = 0 for line in el:
25
3678
by: skull | last post by:
Hi everybody, it is my first post in this newsgroup. I am a newbie for python though I have several years development experience in c++. recently, I was stumped when I tried to del item of a list when iteration. here is the wrong way I did: lst = for i in lst: print i if i == 2:
6
4786
by: Ravi | last post by:
Hi All, I am trying to execute a select statement using the DBI module of perl in a for loop. I am getting a strange behaviour, the select statement is excuting correctly only for the last element in the for loop. I am including the portion of the code : #Get the connection to the database my $dbh = &getConnection(); my @acodes;
12
2389
by: jason | last post by:
Access 2000: I have a customer-inventory table I need to loop through and compile a list of all the inventory items the customer is tracking. The problem I am finding is that a simple loop will pull out the customer details each time their is an inventory item listed....I need to get the customer out ONCE and list his items....is there an...
73
4577
by: Claudio Grondi | last post by:
In the process of learning about some deeper details of Python I am curious if it is possible to write a 'prefix' code assigning to a and b something special, so, that Python gets trapped in an endless loop in a line with: if a==b: print 'OK' I mean, it would be of much help to me on my way to understanding Python to know how such prefix...
6
4919
by: Horta | last post by:
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 = b =
1
1145
by: rshepard | last post by:
Haven't found an answer to my question in the books and other docs I have available, so I am asking here. I have three lists of data retrieved from database tables. I want to cycle through all three lists using nested FOR loops. What is the behavior if there are no data in the list used in the middle loop? Example: for a in types:
23
1710
by: Sacred Heart | last post by:
Hi, I'm new to Python and have come across a problem I don't know how to solve, enter com.lang.python :) I'm writing some small apps to learn the language, and I like it a lot so far. My problem I've stumbled upon is that I don't know how to do what I want. I want to do a loop in a loop. I think.
3
1607
by: Brad | last post by:
Hi folks, I'm still fairly new to programming in python and programming in general. A friend of mine is in a CompSci 101 course and was working on a slider game when he encountered a problem. We eventually figured out what the problem was and built a test case to help solve it, but I can't for the life of me figure out the why behind it. I...
0
8299
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
8163
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6545
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5667
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5355
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3799
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3813
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1397
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1127
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.