0

I am new to Python so please bear me and see my problem and help me to solve my problem.

Here I just tried to Reverse a list using reverse() method. But I left with confusion after seeing the outputs.

Here are the ways what I tried.

a=[2,3,4,5,6]
a.reverse()
print(a)     #print1
print(a.reverse()) #print2
b=a.reverse()
print(b)     #print3

Output for the above code is:

[6, 5, 4, 3, 2]
None
None

Here I don't understand why I am getting None as output for print2 and print3

  • reverse method reverses the list in-place and it returns None –  Jan 12 '22 at 07:24
  • This means that the list will be reversed itself. If you want to just get the list backwards add [::-1] to the end – smal Jan 12 '22 at 07:27
  • Sorry can you please elaborate your explanation. – Vijayakash Allenki Jan 12 '22 at 07:27
  • `.reverse` changes the list object by itself, and returns `None`. After you call it, you can use the list. You should not try to use the return value, because the return value will just be `None`. You should call `.reverse` on the list, and then use the list. Like in your first `print`. There is nothing to explain, so we can't answer "why". It was designed that way because the designers of the language want it to work that way. – Karl Knechtel Jan 12 '22 at 07:28

0 Answers0