0

I have this function.

def creates(n): 
    output = []
    for i in range(1, n+1):
        output.append(range(-1, i-1))
    return output

I want it to return [[1],[2,1],[3,2,1],[4,3,2,1]] when I print creates(4) without using a reverse function in the code. I know it's possible and I feel like I am using append incorrectly but I don't know where the issue is. Thanks!

2

1 Answer 1

6

You mean to do:

output.append(range(i, 0, -1))

Since range(4, 0, -1) (for example) returns [4, 3, 2, 1].

Note that this could be written in fewer lines as a list comprehension:

def creates(n):
    return [range(i, 0, -1) for i in range(1, n+1)]
Sign up to request clarification or add additional context in comments.

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.