Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

Modifying a list in reverse order

Guys I want to know why the code below could not modify the list in reverse order I am getting [8, 7, 6, 5, 5, 6, 7, 8] when I run the code instead of [8,7,6,5,4,3,2,1]. Please kindly assist

x = [1, 2, 3, 4, 5, 6, 7, 8]

for num in range(len(x)):

    x[num] = x[-(num + 1)]

print(x) 

[MOD: added ```python formatting -cf]

1 Answer

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,454 Points

Hey Sunday Okechukwu,, good question! The issue centers on the list x being modified in-place. By the time the loop gets to the later values, the original numbers have been overwritten. Here is a modified execution of your code:

>>> x = list(range(1, 9))
>>> x
[1, 2, 3, 4, 5, 6, 7, 8]
>>> for num in range(len(x)):
...     print(f"setting x[{num}] equal to x[{-(num + 1)}]")
...     x[num] = x[-(num + 1)]
...     print(f"new x: {x}")
... 
setting x[0] equal to x[-1]
new x: [8, 2, 3, 4, 5, 6, 7, 8]
setting x[1] equal to x[-2]
new x: [8, 7, 3, 4, 5, 6, 7, 8]
setting x[2] equal to x[-3]
new x: [8, 7, 6, 4, 5, 6, 7, 8]
setting x[3] equal to x[-4]
new x: [8, 7, 6, 5, 5, 6, 7, 8]
setting x[4] equal to x[-5]
new x: [8, 7, 6, 5, 5, 6, 7, 8]
setting x[5] equal to x[-6]
new x: [8, 7, 6, 5, 5, 6, 7, 8]
setting x[6] equal to x[-7]
new x: [8, 7, 6, 5, 5, 6, 7, 8]
setting x[7] equal to x[-8]
new x: [8, 7, 6, 5, 5, 6, 7, 8]

Notice how after x is 3, the values with x do not change since the list is now symmetrical.

There are two ways around this. Use a copy of the list to hold the original values:

>>> x = list(range(1, 9))
>>> x
[1, 2, 3, 4, 5, 6, 7, 8]
>>> for idx, num in enumerate(x[:], start=1):
...     print(f"setting x[{-idx}] equal to {num}")
...     x[-idx] = num
...     print(f"new x: {x}")
... 
setting x[-1] equal to 1
new x: [1, 2, 3, 4, 5, 6, 7, 1]
setting x[-2] equal to 2
new x: [1, 2, 3, 4, 5, 6, 2, 1]
setting x[-3] equal to 3
new x: [1, 2, 3, 4, 5, 3, 2, 1]
setting x[-4] equal to 4
new x: [1, 2, 3, 4, 4, 3, 2, 1]
setting x[-5] equal to 5
new x: [1, 2, 3, 5, 4, 3, 2, 1]
setting x[-6] equal to 6
new x: [1, 2, 6, 5, 4, 3, 2, 1]
setting x[-7] equal to 7
new x: [1, 7, 6, 5, 4, 3, 2, 1]
setting x[-8] equal to 8
new x: [8, 7, 6, 5, 4, 3, 2, 1]

Or, you may use a slice to directly invert a list using a negative step:

>>> x = list(range(1, 9))
>>> x
[1, 2, 3, 4, 5, 6, 7, 8]
>>> x = x[::-1]
>>> x
[8, 7, 6, 5, 4, 3, 2, 1]

Post back if you need more help. Good luck!!