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

  • SEARCH
  • Home
  • 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 9099895
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T00:43:57+00:00 2026-06-17T00:43:57+00:00

Possible Duplicate: How to iterate over two lists – python I want to iterate

  • 0

Possible Duplicate:
How to iterate over two lists – python

I want to iterate over two items at the same time, an iteration that in my mind looks like this:

for elem1 in list 1 and for elem2 in list2:
    do something to elem1; do something to elem2

This syntax, however, is not acceptable. To be clear, I’m not talking about nested for loops, because then I’d be iterating over an entire list for every element in the first list. I want to iterate over the two lists (or whatever) in tandem. Is there a pythonic way to do this?

  • 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-06-17T00:43:59+00:00Added an answer on June 17, 2026 at 12:43 am

    Use zip():

    for elem1, elem2 in zip(list1, list2):
    

    If one of these lists is longer than the other, you won’t see the elements beyond the length of the shorter list.

    On python 2, zip() results in a copy of both lists zipped together, and for large lists that can be a memory burden. Use itertools.izip() for such larger lists, it returns an iterator instead. On python 3, zip() itself already returns an iterator.

    If you need to loop over the longest list instead (and fill in a filler value for the missing shorter list elements), use itertools.izip_longest() instead:

    from itertools import izip_longest
    
    for elem1, elem2 in izip_longest(list1, list2):
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: reading lines 2 at a time In python we can iterate over
Possible Duplicate: Iterate through two associative arrays at the same time in PHP I
Possible Duplicate: how can I iterate through two lists in parallel in Python? i
Possible Duplicate: How do I iterate over each Entry in a Map? I want
Possible Duplicate: How to iterate through two lists in parallel? I have 2 lists:
Possible Duplicate: iterate through pairs of items in python list I have a list
Possible Duplicate: How do I iterate over each Entry in a Map? How can
Possible Duplicate: How do I iterate over cin line by line in C++? I
Possible Duplicate: iterator validity ,after erase() call in std::set When I iterate over a
Possible Duplicate: Iterate an iterator by chunks (of n) in Python? Say I have

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.