Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (1.6k points)

I have these two iterables:

bee=(5,6,7)

dee=(8,9,1)

for(f,b) in some_iterator(bee,dee):

print "f: ",f,": b: ",b

I want to iterate them in pairs and the output should be like this:

f: 5; b: 8

f: 6; b: 9

f: 7; b: 1

I know on way of doing this which is by iterating over the indices:

for i in xrange(len(bee)):

    print "f: ", bee[i], "; b: ", b[i]

I want another more Pythonic way of doing this.Can anyone suggest?

1 Answer

0 votes
by (25.1k points)

You can do it using the zip function, like this:

for x, y in zip(a, b):

    print(x, y)

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
asked Jul 20, 2019 in Java by Anvi (10.2k points)

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...