-1
os = ['Windows', 'macOS', 'Linux']
print('Original List:', os)
o=os.reverse()
print('Updated List:', o)

the output is None! why doesnt the assignment of reverse to o gives me a reversed list?

i also used

os.reverse

and then simply printed it and it worked! then why does the assignment of the same to o gives me nothing? pretty confused over it

  • 1
    Does this answer your question? [list.reverse does not return list?](https://stackoverflow.com/questions/4280691/list-reverse-does-not-return-list) – jonrsharpe May 26 '20 at 10:58
  • The `reverse` method doesn't return a list. It modifies the list in-place. – bumblebee May 26 '20 at 11:01

1 Answers1

0

The list method reverse does not return anything, so assigning a variable using list.reverse() gives you an empty variable.

Try:

os = ['Windows', 'macOS', 'Linux']
print('Original List:', os)
os.reverse()
print('Updated List:', os)

As you can see, the method reverses the list. what does work is:

os = ['Windows', 'macOS', 'Linux']
print('Original List:', os)
o = os
o.reverse()
print('Updated List:', o)
MilanV
  • 72
  • 6