'How to reverse an active for loop when a condition is satisfied in python?
I am trying to work with for loop, and my requirement is to reverse the loop after some iterations. For example,
for i in range(10):
print(i)
if i==5:
'''reverse loop iterating from 5 to 0'''
Is there a way to reverse the loop, or any other loop functions can do this? Thanks in advance
Solution 1:[1]
I guess that this could work as it reverse iterates as soon as the condition is satisfied
for i in range(10):
if i<5:
print(i)
elif i==5:
for j in range(5,0,-1):
print(j)
else:
break
Hope it was helpful
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Hari |
