0

I'm trying to figure out how to print out a list in reverse order using recursion. This is how I reversed a list:

def reverse_list(xs):
    if xs == []:
         return xs
    else:
        new_list = (print_reverse (xs[1:])) + [xs[0]]
        return new_list

and this is how I printed a list in order:

def print_in_order(l, i = 0):
    if i < len(l):
        print (l[i])
        print_in_order(l, i + 1)

I tried just printing out from the new list inside the function like this:

def print_reverse(xs):
    if xs == []:
        return xs
    else:
        new_list = (print_reverse (xs[1:])) + [xs[0]]
    for number in new_list: 
        print (number)

But it raises an error. How do you combine the two processes?

6
  • 2
    Why don't just modify print_in_order? start from len(l) - 1 and check if i >= 0? Commented Oct 2, 2014 at 6:37
  • What do you mean start from len(1) -1? Commented Oct 2, 2014 at 6:42
  • print_in_order starts from i = 0 i.e. from the beginning of the string, print_in_reverse should start from i = len(l) - 1 - from the last character of the string. Side note: Do not use l when it's easy to mix it with 1 Commented Oct 2, 2014 at 6:46
  • I tried adding i = len(l)-1 to the 2nd parameter but it raises an error. Commented Oct 2, 2014 at 6:47
  • why don't use any byway e.g. i = None, and then, in the function's body check if i = None assign it to len(l) - 1? Commented Oct 2, 2014 at 6:48

4 Answers 4

2

You just need to swap two lines:

def print_in_reverse(lst, i=0):
    if i < len(lst):
        print_in_reverse(lst, i + 1)  # Do the recursive call before you
        print (lst[i])              # print instead of after
Sign up to request clarification or add additional context in comments.

Comments

1

One of the simplest way to do this, it's using slicing in this way:

>>> example = [1, 2, 3, 4, 5]
>>> example[::-1]
[5, 4, 3, 2, 1]

..and if you want to print them, just do this:

>>> for number in example:
...     print(number)
...     
1
2
3
4
5

EDITED:

>>> example = [1, 2, 3, 4, 5]

>>> def recursive(list, counter=0):
...     if len(list) > counter:
...         print(list[counter])
...         recursive(list, counter+1)
...     return
... 

>>> recursive(example[::-1])
5
4
3
2
1

3 Comments

It's a homework exercise that requires you to use recursion and put it in a defined function form.
How can I modify my print_in_order function to print in reversed order?
You can simply pass to the print_in_order method the sliced reversed list, and iterate on it recursively to print it out.
0

Something like that (print_in_order adaptation):

# print l in reverse order, starting from i
# note, that we're allowed not to specify i
def print_in_reverse(l, i = None):
    # if i is not specified    
    if (i is None):
        i = len(l) - 1 

    # we're printing in reverse: 
    # we have to check for beginning of the string instead of ending
    # that's why check for 0 
    if (i >= 0):
        print (l[i])
        print_in_reverse(l, i - 1) # reverse direction: i - 1 instead of i + 1

Comments

0

If you want just print it in reverse order and you don't want to change original list you can just modify print_in_order() like this:

#second parameter is length of list
def print_in_reversed(l, i): 
    if i > 0:
        print (l[i-1])
        print_in_order(l, i - 1)

And you will call it with this example

example = [1,2,3,4,5]    
print_in_order(example, len(example))

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.