-1

I am trying to reverse a list using append and remove method. But there is a problem in my method of doing this solution. Perhaps anyone give me a better explanation

list_in_order = [1,2,3,4,5,6]
def reverse(list):
    r=[]
    for i in list:
        print (i)
        r.append(i)
        list_in_order.remove(i)
    return r

print(reverse(list_in_order))
Paul Rooney
  • 20,879
  • 9
  • 40
  • 61
Zack Choon
  • 109
  • 11
  • Dont remove elements from the original list, just build a new one. You are removing elements from your list whilst iterating over it. That never ends well. – Paul Rooney Jan 15 '19 at 01:14
  • 2
    Also, don't name your variables `list`. – Pika Supports Ukraine Jan 15 '19 at 01:15
  • You are breaking a fundamental programming rule - don't modify an array (list) you are iterating over. Also, you are iterating over the list from start to end and to reverse, you need to go the other way. – see sharper Jan 15 '19 at 01:16

4 Answers4

1

You can't use list.append, as that appends to the end of the list. What you want is to append to the start of the list. For that use list.insert. For more details see the list docs.

list_in_order = [1,2,3,4,5,6]

def reverse(L):
    r = []
    for i in L:
        r.insert(0, i)
    return r

print(reverse(list_in_order))

The other option is of course to use slice notation.

>>> [1,2,3,4,5,6][::-1]
[6, 5, 4, 3, 2, 1]

To make some constructive criticisms of the code in your question

  1. Don't refer to variables outside of the function, unless you really intend to use globals. list_in_order.remove(i) refers to your global variable, not your function argument (although they ultimately refer to the same thing in this case). It may have been a typo but its worth pointing out that it's not quite right.
  2. Don't use variable names that hide built in types. list, dict, tuple, set etc... These are not good names for variables as they will hide those types from further use in the scope that variable exists in and it may be difficult to find the source of the errors you get as a result.
  3. Make your functions do one of two things; either modify the input (called in place modification) and return None or create a new collection and return that.
  4. Don't iterate over a collection while modifying it. See linked dupe for elaboration.
Paul Rooney
  • 20,879
  • 9
  • 40
  • 61
0
In [11]: list_in_order = [1,2,3,4,5,6]

In [12]: list(reversed(list_in_order))
Out[12]: [6, 5, 4, 3, 2, 1]
lexual
  • 46,172
  • 2
  • 15
  • 14
0

Do you want [6, 5, 4, 3, 2, 1] for the result? If so, just use list_in_order[::-1]

If you must use the append and remove,

list_in_order = [1,2,3,4,5,6]

def reverse_list(target_list):
    copied_list = target_list.copy()
    r=[]
    for i in reversed(target_list):
        r.append(i)
        copied_list.remove(i)
    return r
  • don't remove list elements when it in loop.
  • don't use 'list' for variable/parameter name.
Yuda
  • 343
  • 4
  • 15
0

I would make it like this :

list_in_order = [1,2,3,4,5,6]
    def reverse(list):
        counter = len(list)
        for i in range(0,counter/2):
            print(i)
            list[i],list[counter-i] = list[counter-i], list[i]
        return list

print(reverse(list_in_order))

This is a one way to do it, but you can do it using a recursion :)

StyleZ
  • 1,276
  • 3
  • 11
  • 27