0
list.reverse() prints None??
list = [1, 2, 3] print(list.reverse() ) Why this code prints None instead of [3, 2, 1]
5 Respuestas
+ 7
Omar Ashraf 🇪🇬 it printed none because when you print list.reverse() it reverses your list and copy it back to your list which means list.reverse() gives you nothing back but changes the list that is [1,2,3] first and after reversing it copy the reversed list back to list now your list will become [3,2,1]
+ 6
They are called as inplace methods.
Those are methods, that are used to modify their object and their return values are often not important.
Only mutable objects can have inplace methods.
Methods of immutable objects, such as string often return a modified copy of their value instead, example:
string1 = "50 60"
stringlist1 = string1.split(" ")
print(string1) #50 60
print(stringlist1) #['50', '60']
+ 4
Instead of print(list.reverse())
Use
list.reverse()
Print(list)
+ 2
Thank you so much Chirag Kumar
I got it!
+ 1
Thanks Chirag Kumar , it worked!
Still not sure why it printed None in the first code.