Approach 1: Use the built-in function reversed() to reverse the list
Approach 2: Use for loop and the len() function
Get the size of a list using the len(list1) function
Use for loop and reverse range() to iterate index number in reverse order starting from length-1 to 0. In each iteration, i will be reduced by 1
In each iteration, print list item using list1[i]. i is the current value if the index
Solution1:Using a reversed() function and for loop
list1 = [10, 20, 30, 40, 50]
# reverse list
new_list = reversed(list1)
# iterate reversed list
for item in new_list:
print(item)
Solution2:Using for loop and the len() function
list1 = [10, 20, 30, 40, 50]
# get list size
# len(list1) -1: because index start with 0
# iterate list in reverse order
# star from last item to first
size = len(list1) - 1
for i in range(size, -1, -1):
print(list1[i])
Hint:
Approach 1: Use the built-in function
reversed()
to reverse the listApproach 2: Use for loop and the
len()
functionlen(list1)
functionfor
loop and reverserange()
to iterate index number in reverse order starting from length-1 to 0. In each iteration, i will be reduced by 1list1[i]
. i is the current value if the indexSolution1:Using a
reversed()
function andfor
loopSolution2:Using for loop and the
need an explanation for this answer? contact us directly to get an explanation for this answerlen()
function