0

I have the function written which should work good theoretically but the function seems to be changing the original list as well where i don't want the original to reversed but instead show the variable a in reverse only

def listreverse(list_input):
    length = 0
    list_swapped1 = list_input
    for i in range(len(list_input)):
        length += 1
    swap = (length + 1) // 2
    for i in range(swap):
        temp1 = list_swapped1[0 + i]
        temp2 = list_swapped1[(length - 1) - i]
        list_swapped1[0 + i] = temp2
        list_swapped1[(length - 1) - i] = temp1
    return list_swapped1


mylist = [1, 2, 8, 4, 5]
a = listreverse(mylist)
print(a)
print(mylist)

OUTPUT

[5, 4, 8, 2, 1]
[5, 4, 8, 2, 1]

thanks in advance

0 Answers0