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 trialShady Habashy
2,139 PointsHi.. I tried the .reverse() method but it always returns ==> None why is this happening ?
Hi.. I tried the .reverse() method to reverse my list but it always returns ==> None why is this happening ?
Shady Habashy
2,139 Pointsmy_list = [1, 2, 3, 4, 5]
rev_list = my_list.reverse()
print(rev_list)
2 Answers
Josh Keenan
20,315 PointsSo list.reverse()
uses in place modification, what this means is that the method does not return a new object and instead modifies the already existing one.
So if you want to create a new list object that is the reversed version of your original list, use reversed()
and pass in your list, this will create a new object.
my_list = [1, 2, 3, 4, 5]
my_list.reverse()
print("list now: " + str(my_list))
This is what you must to if you want to use list.reverse()
Shady Habashy
2,139 Pointsok got it . Thank you :)
jessicamurr
3,313 PointsHi there! Thanks for answering the same question I had. However, I'm a bit confused. Why do we have to use str() for my_list. I know that not using it makes the return None or have an error pop up, but I'm confused as to why we have to make a list a string. Thanks ahead of time!
Jay zhang
4,109 Pointshello, just to answer the jessicamurr's question, if you test the code you can quickly understand that :
You can only concatenate same value to each other, in this case since "list now: " is a string, you can't concatenate a list to a string
Muhammad khan
Python Development Techdegree Student 1,836 Pointscan someone please explain why am I not getting a reversed list printed? getting some strange out: <list_reverseiterator object at 0x7feb3cf019b0>
my_list = [1, 2, 3, 4, 5]
reversed_list = reversed(my_list)
print(reversed_list)
Alfonso Quijano
15,137 Pointstry "reversed ()" like this: "obj = [6, 7, 8, 9, 10] inverted = reversed (obj) print (inverted) " and I got this result that I understand was stored in that place but it doesn't show me the inverted list. "<list_reverseiterator object at 0x00000268A9B44E20>"
Josh Keenan
20,315 PointsMuhammad khan you need to do
my_list.reverse()
Leonard Peris
15,784 PointsMuhammad khan if you want to obtain a list that is the reverse of your list, without modifying your existing list, see the following code example.
my_list = [1, 2, 3, 4, 5]
reverse_list = list(reversed(my_list))
print(reverse_list)
reversed()
returns a <list_reverseiterator object>
. In order to obtain a list, we have to pass the return value to list()
.
Alternatively, you can use slices to obtain the reverse of a list without modifying the original.
my_list = [1, 2, 3, 4, 5]
reverse_list = my_list[::-1]
print(reverse_list)
Josh Keenan
20,315 PointsJosh Keenan
20,315 PointsCan you post some code please so I can try and understand what's going on.