473,804 Members | 3,373 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Iterating two arrays at once

Hi there,

just trying to figure out how to iterate over two array without
computing the len of the array:

A = [1,2,3]
B = [4,5,6]
for a,b in A,B: # does not work !
print a,b

It should print:

1,4
2,5
3,6

Thanks !
Aug 29 '08 #1
4 1674
mathieu a écrit :
Hi there,

just trying to figure out how to iterate over two array without
computing the len of the array:

A = [1,2,3]
B = [4,5,6]
for a,b in A,B: # does not work !
print a,b

It should print:

1,4
2,5
3,6
for a, b in zip(A, B):
print a, b

or, using itertools (which might be a good idea if your lists are a bit
huge):

from itertools import izip
for a, b in izip(A, B):
print a, b
Aug 29 '08 #2
Am Fri, 29 Aug 2008 03:35:51 -0700 schrieb mathieu:>
A = [1,2,3]
B = [4,5,6]
for a,b in A,B: # does not work !
print a,b

It should print:

1,4
2,5
3,6
Hey,

zip is your friend:

for a,b in zip(A,B):
print a,b

does what you want. If you deal with big lists, you can use izip from
itertools, which returns a generator.

from itertools import izip
for a,b in izip(A,B):
print a,b

HTH

Matthias
Aug 29 '08 #3
On Aug 29, 12:46 pm, Matthias Bläsing <matthias.blaes ...@rwth-
aachen.dewrote:
Am Fri, 29 Aug 2008 03:35:51 -0700 schrieb mathieu:>
A = [1,2,3]
B = [4,5,6]
for a,b in A,B: # does not work !
print a,b
It should print:
1,4
2,5
3,6

Hey,

zip is your friend:

for a,b in zip(A,B):
print a,b

does what you want. If you deal with big lists, you can use izip from
itertools, which returns a generator.

from itertools import izip
for a,b in izip(A,B):
print a,b
Thanks all !
Aug 29 '08 #4
mathieu a écrit :
(snip solution)
Thanks all !
FWIW, this has been discussed here *very* recently (a couple hours ago).
Look for a thread named "iterating over two arrays in parallel?", and
pay special attention to Terry Reedy's answer.
Aug 29 '08 #5

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

Similar topics

9
10718
by: matthurne | last post by:
I need to send just an array to a function which then needs to go through each element in the array. I tried using sizeof(array) / sizeof(array) but since the array is passed into the function, sizeof(array) is really sizeof(pointer to first element in array) and therefore doesn't solve my problem. If I can't calculate the size of the array, can I go through the elements without knowing the size and somehow test whether I'm off the end...
34
4247
by: Christopher Benson-Manica | last post by:
If an array is sparse, say something like var foo=; foo=4; foo='baz'; foo='moo'; is there a way to iterate through the entire array? --
6
6062
by: Gustaf Liljegren | last post by:
I ran into this problem today: I got an array with Account objects. I need to iterate through this array to supplement the accounts in the array with more data. But the compiler complains when I try to modify the objects in the array while iterating through it. I marked the bugs in this code: // Loop through all previously added accounts foreach(Account a in a1) // a1 is an ArrayList { // If name and context is the same if(a.Name ==...
7
6892
by: Joseph Lee | last post by:
Hi All, I am having problem when i am using hashtable to keep an array of bytes value as keys. Take a look at the code snippet below --------------------------------------------------- ASCIIEncoding asciiEncoder = new ASCIIEncoding();
9
1701
by: Ben Rudiak-Gould | last post by:
Background: I have some structs containing std::strings and std::vectors of other structs containing std::strings and std::vectors of .... I'd like to make a std::vector of these. Unfortunately the overhead of the useless copies made each time the vector is resized is too large for me to ignore. I know that rvalue references fix this problem, but I don't think they'll be widely available for years and I need something that works now....
4
2823
RMWChaos
by: RMWChaos | last post by:
The next episode in the continuing saga of trying to develop a modular, automated DOM create and remove script asks the question, "Where should I put this code?" Alright, here's the story: with a great deal of help from gits, I've developed a DOM creation and deletion script, which can be used in multiple applications. You simply feed the script a JSON list of any size, and the script will create multiple DOM elements with as many attributes...
3
5170
by: Robert Mark Bram | last post by:
Hi all, I have some code to iterate through nested associative arrays. Can anyone please tell me what I am doing wrong? :) var dealers = new Array(); var dealer1 = new Array(); dealer1 = "Jefferson Ford Pty Ltd"; dealer1 = "215-217 Normanby Rd South Melbourne VIC 3205";
3
1399
by: Lie | last post by:
When you've got a nested loop a StopIteration in the Inner Loop would break the loop for the outer loop too: a, b, c = , , def looper(a, b, c): for a_ in a: for b_ in b: for c_ in c: print a_, b_, c_
4
2986
by: mh | last post by:
I want to interate over two arrays in parallel, something like this: a= b= for i,j in a,b: print i,j where i,j would be 1,4, 2,5, 3,6 etc.
0
9705
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10567
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10323
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10310
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9138
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6847
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5515
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3809
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2983
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.