Why does this code give this output?? <built-in method reverse of list object at 0x7e3911ffc0> | Sololearn: Learn to code for FREE!

0

Why does this code give this output?? <built-in method reverse of list object at 0x7e3911ffc0>

My code was x=["yg","g","h"] print(x.reverse) Why doesn't it work? Can you please tell me the right code?

4/9/2021 3:33:01 PM

Ayushman Halder

4 Answers

New Answer

+13

reverse-function does reverse the list in place, but does not return something itself. correct code: x=["yg","g","h"] x.reverse() print(x) the example x=["yg","g","h"] print(x.reverse()) results in None because it returns nothing

+6

x. reverse()... better it is print(x. reverse) prints the method itself additionally it is an inplaceMethod as Jan Markus already wrote. to create a new object print(reversed(x)).... not sure at alk

+5

The reverse method needs to be called to do anything. This will work: x=["yg","g","h"] x.reverse() # reverse elements of x. Notice the () brackets. print(x)

+2

Thanks Josh Greig Jan Markus Frogged ☺