+ 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
3 Answers
+ 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
+ 4
a = [1,2,3]
a.reverse()
print(a) #[3,2,1]
+ 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.