+ 1

letters.reverse() is not working i.e., reverse method on list.is this correct?

Python is used here. letters = ['p','q','r','s','p'] When i do below it shows none print(letters.reverse()) Output: none Can anyone check and help me on this

17th Aug 2017, 2:31 AM
Kishore Kumar Vasireddy
Kishore Kumar Vasireddy - avatar
3 Respostas
+ 13
to explain Oscar Albornoz answer, reverse method changes the list and does not return a new list you can do this however: letters = ['p','q','r','s','x','y','z'] print(letters[::-1]) alternatively, you can use the reversed function: print(list(reversed(letters))) this will actually create a shallow copy of the list, and return it, keeping the original list unchanged
17th Aug 2017, 6:10 AM
Burey
Burey - avatar
+ 4
a = [1,2,3] a.reverse() print(a) #[3,2,1]
17th Aug 2017, 4:51 AM
Oscar Albornoz
Oscar Albornoz - avatar
+ 2
Here's the official blurb: https://docs.python.org/3/tutorial/datastructures.html The same situation occurs with the list.sort(), list.remove() and list.insert() methods.
17th Aug 2017, 7:11 AM
David Ashton
David Ashton - avatar