0

I need a code that reverses a list without using both return of reverse(). So it can only modify the inputted list. These are the methods i have tried so far that havent worked:

def reversed(x):
    x[::-1]

and

def reverse(x):
    in_list = [] 
for i in range(1, len(x)+1): 
    x.append(x[-i])

The second code gives me an error of 'name 'x' is not defined'

7
  • 1
    Watch your indentation on your second function Commented Jan 26, 2021 at 18:23
  • You need a code? Do you mean a function? Or a statement? And do you mean you can't use the builtin reversed function or return statement? Commented Jan 26, 2021 at 18:25
  • Do you want a list expression like [x[-(i+1)] for i in range(len(x))] Commented Jan 26, 2021 at 18:26
  • You probably want to run the for loop inside the reverse(x) function. So, you need to indent the for loop appropriately. Commented Jan 26, 2021 at 18:30
  • 1
    Is this for a homework assignment? Seems like one of very few places such a constraint would be placed Commented Jan 26, 2021 at 21:03

4 Answers 4

1

You could assign x to it's reversed value:

x = x[::-1]

This is the same as your first example although it reassigns the variable.

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

5 Comments

You have to use a slice assignment (x[:] = x[::-1]), otherwise you are just rebinding the local name x without affecting the list it originally referred to.
Although in either situation won't x be assigned to the same value after either approach @chepner? Isn't that the intended result? I could be wrong
Actually I see what you're saying although OP isn't very clear on what they want really. I assumed x would be the only variable at hand.
Oh, I was assuming you were supplying the body of the OP's reverse function. If it's the same scope, yes, this is fine.
No this is just an inline statement
0
def reverse_list(x):
    return x[::-1]

x =[1,2,3,4,5]
 
y = reverse_list(x)

print(y)

Output: [5, 4, 3, 2, 1]

Comments

0

First, don't do this. Mutating data structures makes your coworkers hate you. But if you must, consider:

def mutate(lst):
    backward = lst[::-1]
    lst.clear()
    lst.extend(backward)

a = [1,2,3,4]
print(a)
print(id(a))

mutate(a)
print(a)
print(id(a))

The mutate function makes a reversed copy of its inputs. Then it clears the contents of the list that was passed in so that it's empty. Finally, it fills that empty list with the reversed copy. This way the original object is mutated in place and retains its original ID.

Alternatively, if you're extremely resource limited and are willing for the code to be slower in exchange for not having to temporarily duplicate the input list, you could swap the first and last items, then the second and second-to-lsat items, and so on until you reach the middle of the list:

def mutate_by_swapping(lst):
    last_index = len(lst) - 1
    for i in range(len(lst) // 2):
        lst[i], lst[last_index - i] = lst[last_index - i], lst[i]

Comments

0
x = ["a", "b", "c", "d","e"]

z = []

for i in range(len(x)):
    m = len(x) - i -1
    z.insert(i,x[m])

print(z)

Output will be: ['e', 'd', 'c', 'b', 'a']

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.