0

I want to print my list in order, but it keeps printing the first value

def print_order(s):
    if not s: 
        return
    print(s[0])
    print_order(s[:-1])

for example I have a list [1, 2, 3, 4, 5, 6, 7] I want it to be printed out as

    1 
    2 
    3 
    4 
    5 
    6 
    7

2 Answers 2

1

You are taking the last element off instead of the first. Try changing the recursive call's argument to s[1:].

Sign up to request clarification or add additional context in comments.

1 Comment

@12345678 If this (or another) answer helped you, you can "accept" an answer by clicking the checkmark to the left of the answer.
0

The slice s[:-1] is all elements except the last.

You want s[1:], which is all elements except the first.

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.