How can I iterate through two lists in Parallel

0 votes
I have two iterables, and I want to go over them in pairs

foo = (1, 2, 3)
bar = (4, 5, 6)

for (f, b) in some_iterator(foo, bar):
    print "f: ", f, "; b: ", b

The result should be

f: 1; b: 4
f: 2; b: 5
f: 3; b: 6

But that seems somewhat unpythonic to me. Is there a better way to do it?
Apr 17, 2018 in Python by aryya
• 7,460 points
1,424 views

1 answer to this question.

0 votes
You have to use the zip function for your cause

for (f,b) in zip(foo, bar):
    print "f: ", f ,"; b: ", b
answered Apr 17, 2018 by anonymous

Related Questions In Python

0 votes
1 answer

How can I compare the content of two files in Python?

Assuming that your file unique.txt just contains ...READ MORE

answered Apr 16, 2018 in Python by charlie_brown
• 7,720 points
2,655 views
0 votes
1 answer

How can I print Lists in python

print is a function in Python 3.x, so ...READ MORE

answered Oct 24, 2018 in Python by Priyaj
• 58,020 points
849 views
0 votes
2 answers

How can I write a program to add two numbers using functions in python?

there is sum() function as a built ...READ MORE

answered Oct 25, 2020 in Python by anonymous
23,843 views
0 votes
1 answer

How can I combine two dataframes in Python Pandas?

Hi, You can use the following methods to ...READ MORE

answered Jun 25, 2019 in Python by Shabnam
• 930 points
1,267 views
0 votes
1 answer

How do I concatenate two lists in Python?

You can use the + operator to combine them: listone ...READ MORE

answered Dec 3, 2020 in Python by anonymous
• 65,770 points
546 views
0 votes
0 answers

How can I "count from the back" in python lists?

Say I have a list l = ['P','y','t','h','o','n'] I ...READ MORE

Nov 22, 2022 in Python by sarit
• 1,830 points
377 views
+3 votes
7 answers

How can I rename a file in Python?

yes, you can use "os.rename" for that. ...READ MORE

answered Mar 31, 2018 in Python by DareDev
• 6,890 points
20,008 views
+2 votes
2 answers

How can I create a new file in Python?

You can try the below code which ...READ MORE

answered Mar 31, 2018 in Python by anto.trigg4
• 3,440 points
1,304 views
0 votes
1 answer

How can I convert a list of dictionaries from a CSV into a JSON object in Python?

You could try using the AST module. ...READ MORE

answered Apr 17, 2018 in Python by anonymous
3,458 views
0 votes
1 answer

How can I reformat value_counts() analysis in Pandas for large number of columns?

If I were you, I'd do it ...READ MORE

answered Apr 17, 2018 in Python by anonymous
6,693 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP