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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T06:04:08+00:00 2026-06-06T06:04:08+00:00

I am trying to test some other Python code repeatedly, using all possible combinations

  • 0

I am trying to test some other Python code repeatedly, using all possible combinations of values for six different parameters. For each parameter I want to iterate over a range of values with a given minimum, maximum and step.

I managed to write some code like:

for var1 in range(min1, max1, step1):
    for var2 in range(min2, max2, step2):
        for var3 in range(min3, max3, step3):
            for var4 in range(min4, max4, step4):
                for var5 in range(min5, max5, step5):
                    for var6 in range(min6, max6, step6):
                        do_something_with(var1, var2, var3, var4, var5, var6)

But I do not like that the code is so deeply nested.

How can I avoid nesting multiple loops? Can it be done using recursion? How about itertools.product? I wasn’t able to figure out either approach.


See also, more generally: Get the cartesian product of a series of lists?

This question, and some answers, originally showed code for Python 2.x. It has been edited because the fundamental problem persists in 3.x, with the same solution, but xrange no longer exists – range is the drop-in replacement. For more information, see What is the difference between range and xrange functions in Python 2.X?.

  • 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-06T06:04:10+00:00Added an answer on June 6, 2026 at 6:04 am

    Here’s how to use product:

    x1 = range(min1, max1, step1)
    x2 = range(min2, max2, step2)
    x3 = range(min3, max3, step3)
    ...
    
    for v1, v2, v3, v4, v5, v6 in itertools.product(x1, x2, x3, x4, x5, x6):
        do_something_with(v1, v2, v3, v4, v5, v6)
    

    or a bit more compactly:

    ranges = [
        range(min1, max1, step1),
        range(min2, max2, step2),
        range(min3, max3, step3),
        ...
    ]
    
    for v1, v2, v3, v4, v5, v6 in itertools.product(*ranges):
        do_something_with(v1, v2, v3, v4, v5, v6)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to retrofit some tests using Test::More to legacy code and I've bumped
I'm trying to test some legacy code, using Mockito. I want to stub a
I'm trying to test some code in different situations (for different result sets). I've
I am trying to test some of these code here http://ha.ckers.org/xss.html on my code.
I am trying to test out some Dapper stuff using LinqPad. Dapper needs a
I'm a student, and I'm trying to write and run some test code for
I'm currently debugging some fairly complex persistence code, and trying to increase test coverage
I have some test code that i'm just trying to use to figure out
I'm trying to test some streaming file upload/download code that I've just written, but
I'm trying to include some data files in a python package using the setuptools

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.