Can we change Python for loop range (higher limit) at runtime?


No, You can't modify a range once it is created. Instead what you can do is use a while loop instead. For example, if you have some code like:

for i in range(lower_limit, higher_limit, step_size):

# some code
if i == 10:
   higher_limit = higher_limit + 5

You can change it to:

i = lower_limit
while i < higher_limit:
   # some code
   if i == 10:
      higher_limit = higher_limit + 5
   i += step_size

Samual Sam
Samual Sam

Learning faster. Every day.

Updated on: 17-Jun-2020

277 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements