0

I am trying to create a function that will make a multidimensional list taking an input number to choose the amount of dimensions the list will be. This is my code so far:

def createMultiDimList(dimensions, currentDim=0, output=[]):
    if currentDim < dimensions:
        output.append([])
        currentDim += 1
        createMultiDimList(dimensions, currentDim, output[0])
        return output
    else:
        return output

I think this isn't working because the recursion is just putting in the single dimensional list, but I am not sure on that.

4
  • whats your result? Commented Apr 14, 2022 at 17:35
  • Are you unsure if it is working or why it isn't working? Commented Apr 14, 2022 at 17:36
  • @mkrieger1 I am unsure why it isn't working or what I can do to fix it Commented Apr 14, 2022 at 17:41
  • @adirabargil I tested it and the result is a single dimensional array Commented Apr 14, 2022 at 17:41

2 Answers 2

3

You're overcomplicating it IMO. The general idea behind recursion is to solve the easiest case first, and then return the result of the more difficult case in terms of an incrementally easier case.

For this function, the "easy case" is that a 1-dimensional list is []. The incrementally easier case is that an n-dimensional list is an n-1-dimensional list inside a list. Hence:

>>> def n_dim_list(n: int) -> list:
...     if n == 1:
...         return []
...     return [n_dim_list(n-1)]
...
>>> n_dim_list(4)
[[[[]]]]

If you wanted to complicate it a bit to make it a more interesting example of recursion, you could define a filler value for the 1-dimensional lists and the length of each list:

>>> def n_dim_list(n: int, length: int = 0, value = None) -> list:
...     if n == 1:
...         return [value] * length
...     return [n_dim_list(n-1, length, value) for _ in range(length or 1)]
...
>>> n_dim_list(4)
[[[[]]]]
>>> n_dim_list(4, 2, 0)
[[[[0, 0], [0, 0]], [[0, 0], [0, 0]]], [[[0, 0], [0, 0]], [[0, 0], [0, 0]]]]
Sign up to request clarification or add additional context in comments.

3 Comments

So is the problem with my code that the appending just appends a list to the list, then when I pass that list to the function it's just passing the list within the list and it'll just return a single dimension list in the end?
The big problem that I see with your code is that you're using a mutable default argument, and mutating it. (This is the most common Python "gotcha" -- see docs.python-guide.org/writing/gotchas) You don't need to do either of those things, though -- there's no reason to pass the list into the function, and there's no reason to mutate it as you go. As I said, you're overcomplicating it, and unnecessary complexity is where most bugs come from. :)
perfect answer!
1

as @Samwise wisely said, you have simpled awesome approuch like his, specifically for your issue, you need to send down nested list instead of appending item and returning it:

def createMultiDimList(dimensions, currentDim=0, output=[]):
    if currentDim < dimensions:
        currentDim += 1
        createMultiDimList(dimensions, currentDim, [output])
        return output
    else:
        return output

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.