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 Python Sequences Sequence Operations Sequence Operations Cheat Sheet

Shady Habashy
Shady Habashy
2,139 Points

Hi.. 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 ?

Josh Keenan
Josh Keenan
19,652 Points

Can you post some code please so I can try and understand what's going on.

Shady Habashy
Shady Habashy
2,139 Points

my_list = [1, 2, 3, 4, 5]

rev_list = my_list.reverse()

print(rev_list)

2 Answers

Josh Keenan
Josh Keenan
19,652 Points

So 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
Shady Habashy
2,139 Points

ok got it . Thank you :)

jessicamurr
jessicamurr
3,313 Points

Hi 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!

hello, 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
seal-mask
.a{fill-rule:evenodd;}techdegree
Muhammad khan
Python Development Techdegree Student 1,836 Points

can 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
Alfonso Quijano
15,137 Points

try "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
Josh Keenan
19,652 Points

Muhammad khan you need to do

my_list.reverse()
Leonard Peris
Leonard Peris
15,784 Points

Muhammad 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)