Exercise 75

Write a Python algorithm to reverse a list without using the reverse() method

Solution

def reverseList (L):
# initialization of the reversed list
reverseL = []
for i in range (0, len (L)):

# insert L[i] at first position in reversL
reverseL.insert (0, L [i])
return reverseL
#Example
L = [22, 31, 17, 53]
print (reverseList (L)) # output: [53, 17, 31, 22]
Younes Derfoufi
my-courses.net

Leave a Reply