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 8343751
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T06:09:40+00:00 2026-06-09T06:09:40+00:00

I need to iterate on two lists in the following way: Pseudo code: j=1

  • 0

I need to iterate on two lists in the following way:

Pseudo code:

j=1
for i=1 to n:
   print a[i], b[j]
   while b[j+1] <= a[i]:
      j++
      print a[i], b[j]

For example:

a = [1 3 5 7]
b = [2 4 9] 

Desired output:

1 2
3 2
5 2
5 4
7 4

How do you do it cleanly in python?

  • 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-09T06:09:42+00:00Added an answer on June 9, 2026 at 6:09 am

    Your pseudo code will almost work in Python. Some working code that does what you want is:

    a = [1, 3, 5, 7]
    b = [2, 4, 9] 
    j = 0
    for i in range(len(a)):
        print a[i], b[j]
        while j<len(b)-1 and b[j+1] <= a[i]:
            j += 1
            print a[i], b[j]
    

    Note the few changes to make it work in Python:

    1. When declaring the list, commas are required between items.
    2. List indices start at 0, so both i and j should start there.
    3. len(a) returns the length of a (4 in this case), and iterating i through range(len(a)) executes the loop for each integer from 0 to len(a)-1, which is all of the indices in a.
    4. The ++ operation is not supported in Python, so we use j +=1 instead.
    5. We have to avoid using out of bounds indices of b, so we test to make sure j will be in bounds before incrementing it.

    This code can be made more pythonic by iterating through the list as follows:

    a = [1, 3, 5, 7]
    b = [2, 4, 9] 
    j = 0
    for element in a:
       print element, b[j]
       while j<len(b)-1 and b[j+1] <= element:
          j += 1
          print element, b[j]
    

    In general, you probably don’t want to just print list elements, so for a more general use case you can create a generator, like:

    def sync_lists(a, b)
        if b:
            j = 0
            for element in a:
                yield (element, b[j])
                while j<len(b)-1 and b[j+1] <= element:
                    j += 1
                    yield (element, b[j])
    

    And then you can print them as before with

    a = [1, 3, 5, 7]
    b = [2, 4, 9]
    for (e1, e2) in sync_lists(a, b):
        print e1, e2
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say I have components that I need to iterate through, for example From and
I have a 3D array in Python and I need to iterate over all
I have two sorted arrays, Haystack and Needles . I need to iterate over
I'm doing code generation in Jinja2 and I frequently want to iterate through two
I sometimes need to iterate a list in Python looking at the "current" element
I need to iterate through all byte values (-128 to 127 inclusive). I could
I need to iterate through nested entities in a Twig template. Entity A ->
I need to iterate over a recordset from a stored procedure and execute another
I have 5 buttons and I need to iterate over them to carry out
I have an table. I need to iterate, for each(datarow r in datatable.rows){ foreach(datacolumns

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.