How to print Python for loop in backwards? you can use reversed() functions to implement the loop from backward direction. usually, Python for loop is implemented in a forward direction however sometimes you would be required to implement for loop in the backward direction. You can easily achieve this by using reversed() function and the range() function, with the negative step.
In this article, I will explain using reversed() and range() functions how we can implement the loop from the back side rather than the front side with examples.
1. Quick examples of Implementing For Loop in Backwards
Following are the quick examples of implementing For Loop in Backwards.
Following are the examples of for loop in backwards
# Example 1: Get the loop iteration in Backwards
# Using reversed()
print(“Get the loop in backwards”)
for i in reversed(range(1,6)):
# Example 2: Get the for loop in backwards using range()
print(“Get the loop in backwards”)
for i in range(6, 0, -1):
# Example 3: Get the for loop in backwards using range() & len()
# Initialize the list
list=[10, 20, 50, 30, 40, 80, 60]
print(“Get the loop in backwards”)
for i in range(len(list) -1, -1, -1):
# Example 4: Get the loop iterations in Backwards
# using slicing
list=[10, 20, 50, 30, 40, 80, 60]
for i in list[-1::-3]:
# Example 5: Get the for loop index in backwards using range() & len()
# Initialize the list
list=[10, 20, 50, 30, 40, 80, 60]
print(“Get the loop index in backwards”)
for i in range(len(list) -1, -1, -1):
# Example 6: Get the index and items of list in backward direction.
list1=[10, 20, 50, 30, 40, 80, 60]
print(“Get the loop in backwards”)
for i, item in reversed(list(enumerate(list1))):
2. Use reversed() Function Get For Loop in Backwards
Python provides easiest way to implement the loop iteration in backward directions using reversed() function. Usually loop iterations start from front side if you want to get the loop iterations from back side rather than the front side you can use reversed() function. Pass range() function into reversed() function will reverse the range() function without any modification and then iterate reversed() function using for loop. This syntax will return the values in backward direction i.e. from back side.
# Get the loop iteration in Backwards
# Using reversed()
print(“Get the loop in backwards”)
for i in reversed(range(1,6)):
print(i)
Yields below output.
3. Using range() Get For Loop in Backwards
Python range() function is used to generate a sequence of numbers within a given range. You can use the range() function with a negative step value to implement the loop iterations from in backside rather than the front side. For example,
# Get the for loop in backwards using range()
print(“Get the loop in backwards”)
for i in range(6, 0, -1):
print(i)
Yields below output.
4. Iterate List using For Loop to Get Values in Backwards
You can use range() function along with the len() function to get the loop iteration in the backward direction. You can specify a range with starting value len(list) -1, ending value -1, and incrementing value -1 to help by getting list values from the last index. Let’s iterate over the range() function to get the list values from from the backward direction.
# Get the for loop in backwards using range() & len()
# Initialize the list
list=[10, 20, 50, 30, 40, 80, 60]
print(“Get the loop in backwards”)
for i in range(len(list) -1, -1, -1):
print(list[i])
Yields below output.
5. Use List Slicing Get For Loop in Backwards
Alternatively, you can use list slicing technique to run the loop in backward direction. Using slicing you can get the specified selection of a list. However, you can use slicing to implement the loop iteration from back side by providing start and step values with negative values.
# Get the loop iterations in Backwards
# using slicing
list=[10, 20, 50, 30, 40, 80, 60]
for i in list[-1::-3]:
print(i)
# Output:
# 60
# 30
# 10
6. Iterate List using For Loop and Get Indexes in Backwards
You can also get the loop index in backward directions using the range() function with a len() function. Let’s iterate over the range(len(list) -1, -1, -1) function to iterate the looping from the backward direction and get the indexes of given list values from the back side.
# Get the for loop index in backwards using range() & len()
# Initialize the list
list=[10, 20, 50, 30, 40, 80, 60]
print(“Get the loop index in backwards”)
for i in range(len(list) -1, -1, -1):
print(i)
# Output:
# Get the loop index in backwards
# 6
# 5
# 4
# 3
# 2
# 1
# 0
7. Get Index and Values in Backwards
Moreover, you can use reversed() function along with list() and enumerate() functions to iterate over the list and get its indexes and corresponding values in the backward direction.
# Get the index and items of list in backward direction.
list1=[10, 20, 50, 30, 40, 80, 60]
print(“Get the loop in backwards”)
for i, item in reversed(list(enumerate(list1))):
print(i, item)
# Output:
# Get the loop in backwards
# 6 60
# 5 80
# 4 40
# 3 30
# 2 50
# 1 20
# 0 10
8. Conclusion
In this article, I will explain using reversed() and range() functions of Python how we can implement the loop from the backward direction with examples.
How to print Python for loop in backwards? you can use reversed() functions to implement the loop from backward direction. usually, Python for loop is implemented in a forward direction however sometimes you would be required to implement for loop in the backward direction. You can easily achieve this by using reversed() function and the range() function, Read More Python, Python Tutorial, Pending review by admin