Exercise 60
Write a Python algorithm that reverse the order of the elements of a list using the reverse() method. Example if L = ['Java', 'Python', 'PHP', 'C ++'], the algorithm returns the list: ['C ++', 'PHP', 'Python', 'Java']
Solution
def reverseList (L):
# invert the list L
L.reverse ()
return L
# Example
L = ['Java', 'Python', 'PHP', 'C ++']
print (reverseList (L)) # the output is: ['C ++', 'PHP', 'Python', 'Java']
Younes Derfoufi
my-courses.net
my-courses.net