Backward Iteration in Python
Hello programmers, in this tutorial we will see how to iterate backward in a list in Python.
In Python, there are many ways in which looping can be performed. Generally, looping follows forward iteration.
But in some cases, we need to perform backward iteration.
Some of the ways, in which backward iteration can be performed are:
- By using the reversed() method.
- By using the range() method
Backward iteration can be understood as traversing or looping the elements in backward order. There are many techniques that we can use to iterate backward. We will see some of them down below using Python.
Method 1 – Using a simple ‘FOR’ loop for a number
We can traverse backward for a number using a simple for loop with the ‘range()’ method.
Given below is an illustration of the following:
#backward iteration for a number def backIterNo(num): print(f'The backward iteration of the number {num} is:') for i in range(num,0,-1): #start from the number, and go backwards till 1 print(i, end = ' ') #driver if __name__ == "__main__": backIterNo(num = 4) #call the function
Output
The backward iteration of the number 4 is: 4 3 2 1
Explanation
We pass the number in the function and using the for loop starting from ‘n’ and ending up to ‘1’, we do the following backward iteration.
Method 2 – Using a simple ‘FOR’ loop for a list
We can traverse backward in a list using the similar fashion discussed above.
Given below is an illustration of the following:
#backward iteration for a list def backIterList(numList): print(f'The backward iteration of the list {numList} is:') for i in range(len(numList),0,-1): #start from the length of the list, and go backwards till 1 print(i, end = ' ') #driver if __name__ == "__main__": numList = [1,2,3,4,5] backIterList(numList) #call the function
Output
The backward iteration of the list [1, 2, 3, 4, 5] is: 5 4 3 2 1
Explanation
We pass the list in the function and traverse backward from the length of the list.
Method 3 – Using ‘reversed()’ method for a number
Given below is an illustration of the following:
#backward iteration for a number def backIterNo(num): print(f'The backward iteration of the number {num} is:') for i in reversed(range(num+1)): #start from the number, and go backwards till 1 print(i, end = ' ') #driver if __name__ == "__main__": backIterNo(num = 4) #call the function
Output
The backward iteration of the number 4 is: 4 3 2 1 0
Explanation
We pass the number in the function and using the ‘reversed()’ function, we start iterating through the number from backward.
Method 4 – Using ‘reversed()’ method for a list
Given below is an illustration for the following:
#backward iteration for a list def backIterList(numList): print(f'The backward iteration of the list {numList} is:') for i in reversed(numList): #start from the number, and go backwards till 1 print(i, end = ' ') #driver if __name__ == "__main__": numList = [1,2,3,4,5] backIterList(numList) #call the function
Output
The backward iteration of the list [1, 2, 3, 4, 5] is: 5 4 3 2 1
Explanation
We pass the list in the function and by using the ‘reversed()’ method, we iterate it backward and print the list elements.
Method 5 – Using List Comprehension for a list
Given below is an illustration of the following:
#backward iteration for a list def backIterList(numList): print(f'The backward iteration of the list {numList} is: {[i for i in range(len(numList),0,-1)]}') #driver if __name__ == "__main__": numList = [1,2,3,4,5] backIterList(numList) #call the function
Output
The backward iteration of the list [1, 2, 3, 4, 5] is: [5, 4, 3, 2, 1]
Explanation
We use list comprehension for iterating backward in the following list.
Method 6 – Using List Slicing
Given below is an illustration of the following:
#backward iteration for a list def backIterList(numList): print(f'The backward iteration of the list {numList} is: {numList[::-1]}') #driver if __name__ == "__main__": numList = [1,2,3,4,5] backIterList(numList) #call the function
Output
The backward iteration of the list [1, 2, 3, 4, 5] is: [5, 4, 3, 2, 1]
Explanation
We start slicing the list from the ‘-1’ position and iterate it backward until we reach the first element in the list.
Leave a Reply