12345678
12345678

Reputation: 1

python 3 recursive function that prints in order

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

Upvotes: 0

Views: 52

Answers (2)

molbdnilo
molbdnilo

Reputation: 66371

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

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

Upvotes: 0

lily
lily

Reputation: 2998

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

Upvotes: 1

Related Questions