0

How to use the reverse method on a list ?

30th Jul 2020, 11:11 AM
Ramzi Mzr
Ramzi Mzr - avatar
2 Respostas
+ 7
reverse() is a list method that does reversing inplace, it does not return the result of the reversing, so if you use this: lst = [1,2,3] print(lst.reverse()) # outputs is None # it could be solved like this: lst = [1,2,3] lst.reverse() print(lst) # there is also an other way possible by using reversed() and a comprehension lst = [1,2,3] [print(i,end=', ') for i in reversed(lst)]
30th Jul 2020, 2:12 PM
Lothar
Lothar - avatar
0
list.reverse() Add your trouble with that clearly so that you get clear answer.. If you not tried, then try now... Edit: Since you asked reverse method: list = [1,2,3,4,5] list.reverse(); print(list) OUTPUT : [5,4,3,2,1]
30th Jul 2020, 11:12 AM
Jayakrishna 🇮🇳