Backward iteration in Python
The iteration of numbers is done by looping techniques in Python. Many techniques in Python facilitate looping. Sometimes we are required to perform the looping backward and having short hands to do so can be quite useful. Let’s discuss certain ways in which this can be done in Python.
What is Backward Iteration?
Backward iteration in Python refers to the process of iterating through a sequence or collection in reverse order, moving from the last element to the first. This is often useful when we need to access elements in the opposite order of their original arrangement. Python provides various mechanisms for backward iteration, such as using negative indexing or employing built-in functions like reversed()
.
Backward Iteration in Python
There are various methods of backward iteration in Python, here we are explaining some generally used methods that we use for Backward iteration in Python those are following.
- Using reversed() Method
- Using range(N, -1, -1)
- Using Slice Syntax
- Using While loop
- Using the join() Function
Using reversed() Method
The simplest way to perform this is to use the reversed function for the for loop and the iteration will start occurring from the rear side than the conventional counting.
Python3
N = 6
print ( "The reversed numbers are : " , end = "")
for num in reversed ( range (N + 1 )) :
print (num, end = " " )
|
Output:
The reversed numbers are : 6 5 4 3 2 1 0
Time complexity: O(N) where N is the value of N
Auxiliary space: O(1) as constant space is used for initialization and printing.
Using range(N, -1, -1)
This particular task can also be performed using the conventional range function which, if provided with the third argument performs the skip and the second argument is used to start from backward.
Python3
N = 6
print ( "The reversed numbers are : " , end = "")
for num in range (N, - 1 , - 1 ) :
print (num, end = " " )
|
Output:
The reversed numbers are : 6 5 4 3 2 1 0
Time complexity: O(N)
Auxiliary space: O(1)
Using Slice Syntax
This particular task can also be performed using the slice syntax which, is used for reversing the list. We used it for reversing the range class in the for loop then we performed the backward iteration.
Python3
N = 6
k = range (N + 1 )[:: - 1 ]
print ( "The reversed numbers are : " ,end = '')
for i in k:
print (i, end = ' ' )
|
Output:
The reversed numbers are : 6 5 4 3 2 1 0
Using While loop
Here the code demonstrates backward iteration in Python, starting from the initialized number `N` (6) and printing the numbers in reverse order until 0 using a while loop.
Python3
N = 6
print ( "The reversed numbers are : " , end = '')
while (N > = 0 ):
print (N, end = ' ' )
N - = 1
|
Output:
The reversed numbers are : 6 5 4 3 2 1 0
Using the join() Function
Here the code performs backward iteration in Python, starting from the given input number N
(6). It creates a string reversed_str
by concatenating the reversed numbers from N
0, separated by spaces. The join()
function is used to convert the reversed range of numbers into a string, and the result is printed.
Python3
N = 6
reversed_str = ' ' .join( map ( str , reversed ( range (N + 1 ))))
print ( "The reversed numbers are :" , reversed_str)
|
Output:
The reversed numbers are : 6 5 4 3 2 1 0
Time complexity: O(N): where N is the value of the given input. The reversed range is generated in O(N) time complexity and converting each number to a string using the map() function takes O(N) time complexity. The join() function also takes O(N) time complexity to concatenate the reversed numbers as a string.
Auxiliary Space: O(N): where N is the value of the given input. The reversed range of numbers and the concatenated string of reversed numbers occupy O(N) space in memory.
Last Updated :
01 Dec, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...