Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 446139
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T21:26:28+00:00 2026-05-12T21:26:28+00:00

I have two iterables, and I want to go over them in pairs: foo

  • 0

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 iterate_together(foo, bar):
    print("f:", f, " |  b:", b)

That should result in:

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

One way to do it is to iterate over the indices:

for i in range(len(foo)):
    print("f:", foo[i], " |  b:", bar[i])

But that seems somewhat unpythonic to me. Is there a better way to do it?


Related tasks:
* How to merge lists into a list of tuples? – given the above foo and bar, create the list [(1, 4), (2, 5), (3, 6)].
* How can I make a dictionary (dict) from separate lists of keys and values? – create the dict {1: 4, 2: 5, 3: 6}.
* Create a dictionary with comprehension – constructing dict using zip in a dict comprehension.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T21:26:28+00:00Added an answer on May 12, 2026 at 9:26 pm

    Python 3

    for f, b in zip(foo, bar):
        print(f, b)
    

    zip stops when the shorter of foo or bar stops.

    In Python 3, zip
    returns an iterator of tuples, like itertools.izip in Python2. To get a list
    of tuples, use list(zip(foo, bar)). And to zip until both iterators are
    exhausted, you would use
    itertools.zip_longest.

    Python 2

    In Python 2, zip
    returns a list of tuples. This is fine when foo and bar are not massive. If they are both massive then forming zip(foo,bar) is an unnecessarily massive
    temporary variable, and should be replaced by itertools.izip or
    itertools.izip_longest, which returns an iterator instead of a list.

    import itertools
    for f,b in itertools.izip(foo,bar):
        print(f,b)
    for f,b in itertools.izip_longest(foo,bar):
        print(f,b)
    

    izip stops when either foo or bar is exhausted.
    izip_longest stops when both foo and bar are exhausted.
    When the shorter iterator(s) are exhausted, izip_longest yields a tuple with None in the position corresponding to that iterator. You can also set a different fillvalue besides None if you wish. See here for the full story.


    Note also that zip and its zip-like brethen can accept an arbitrary number of iterables as arguments. For example,

    for num, cheese, color in zip([1,2,3], ['manchego', 'stilton', 'brie'], 
                                  ['red', 'blue', 'green']):
        print('{} {} {}'.format(num, color, cheese))
    

    prints

    1 red manchego
    2 blue stilton
    3 green brie
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a generator that I want to iterate through at two levels. The
I have two update panels on a page. And I want to update both
I want to write a function that iterates through a list, but takes two
I have a list of two-dimensional points and I want to obtain which of
I have two tables, the structure of the first partially recapitulates, iterates the structure
Have two folders with approx. 150 java property files. In a shell script, how
Have two actionsheet buttons and one modalviewcontroller on mainviewcontroller in application. Now for two
I have two tables images2 and image_data So the goal is to have 1
I have two classes (MVC view model) which inherits from one abstract base class.
I have two files client.php and server.php. The client file send a HTTP request

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.