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’]


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

Leave a Reply