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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T23:23:32+00:00 2026-06-01T23:23:32+00:00

I work with multidimensional structures of arbitrary dimension. I have a Python list of

  • 0

I work with multidimensional structures of arbitrary dimension. I have a Python list of xrange iterators, with each iterator representing an index of a multidimensional array:

indices = [ i, j, k ]

where

i = xrange(1,3)
j = xrange(3,5)
k = xrange(5,7)

To generate all the possible values, I use the following naive recursive code:

def travtree(index,depth):
    "Recursion through index list"
    if depth >= len(indices):
        # Stopping Condition
        print index
    else:
        # Recursion
        currindexrange = indices[depth]
        for currindex in xrange(len(currindexrange)):
            newindex = list(index) # list copy
            newindex.append(currindexrange[currindex])
            travtree(newindex,depth+1)

travtree([],0)

This works fine, but I was wondering, is there a more efficient, Pythonic way to do this? I tried looking in the itertools module but nothing jumps out at me.

  • 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-01T23:23:33+00:00Added an answer on June 1, 2026 at 11:23 pm
    >>> from itertools import product
    >>> i = xrange(1,3)
    >>> j = xrange(3,5)
    >>> k = xrange(5,7)
    >>> indices = [ i, j, k ]
    >>> for item in product(*indices):
            print item
    
    
    (1, 3, 5)
    (1, 3, 6)
    (1, 4, 5)
    (1, 4, 6)
    (2, 3, 5)
    (2, 3, 6)
    (2, 4, 5)
    (2, 4, 6)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a multidimensional array, which I want to initialize in a simple and
I have a multidimensional array that is behaving unexpectedly and I would like to
I have create following string multidimensional array in java. which has top outer array
I have the following multidimensional array: Array ( [0] => Array ( [area] =>
I have a multidimensional array such as Array ( [0] => Array ( [id]
Work on asp.net vs05. I have three type of value Like:IsDesign,IsPrinting,IsInstall they are bit
Work on this small test application to learn threading/locking. I have the following code,
At work, we have migrated from Windows XP to Windows Vista. After the migration,
I need to iterate over an array of arbitrary rank. This is for both
A multidimensional list like l=[[1,2],[3,4]] could be converted to a 1D one by doing

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.