0

list.reverse() prints None??

list = [1, 2, 3] print(list.reverse() ) Why this code prints None instead of [3, 2, 1]

2nd Nov 2019, 11:10 AM
Omar Ashraf 🇪🇬
Omar Ashraf 🇪🇬 - avatar
5 Antworten
+ 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]
2nd Nov 2019, 11:24 AM
Chirag Kumar
Chirag Kumar - avatar
+ 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']
2nd Nov 2019, 11:28 AM
Seb TheS
Seb TheS - avatar
+ 4
Instead of print(list.reverse()) Use list.reverse() Print(list)
2nd Nov 2019, 11:14 AM
Chirag Kumar
Chirag Kumar - avatar
+ 2
Thank you so much Chirag Kumar I got it!
2nd Nov 2019, 11:26 AM
Omar Ashraf 🇪🇬
Omar Ashraf 🇪🇬 - avatar
+ 1
Thanks Chirag Kumar , it worked! Still not sure why it printed None in the first code.
2nd Nov 2019, 11:17 AM
Omar Ashraf 🇪🇬
Omar Ashraf 🇪🇬 - avatar