How to Create a Dynamic Range for Loop in Python?
Last Updated :
23 Jul, 2025
For L
oop is widely used to iterate over sequences in Python. However, there are situations where the range or sequence of the loop needs to be dynamic, and determined at runtime. In this article, we will explore various methods to achieve dynamic range for
loops.
Using range()
with Variables
range()
function is the most common way to create a range for a loop in Python. It accepts start
, stop
, and step
parameters, all of which can be dynamically set using variables.
Python
start = 1
stop = 10
step = 2
# Loop through the range from 'start' to 'stop'
for i in range(start, stop, step):
print(i, end=" ")
Explanation:
range(start, stop, step)
generates numbers starting from start
(inclusive) to stop
(exclusive), incremented by step
.- By assigning values to
start
, stop
, and step
at runtime, we can define the range dynamically.
Let's explore some other methods to create a dynamic range for loop in Python.
Using a Generator Function
Generator functions offer a highly customizable and memory-efficient way to create dynamic ranges. These functions yield values one at a time, as needed by the loop.
Python
def dynamic_range(start, stop, step):
# Continue generating values as long as 'start' is less than 'stop'
while start < stop:
yield start
# Increment 'start' by 'step' for the next iteration
start += step
# Iterate through the values generated by the 'dynamic_range' function
for i in dynamic_range(1, 10, 2):
print(i, end=" ")
Explanation:
dynamic_range()
generator function yields numbers starting from start
and incremented by step
until stop
is reached.- Generators are lazy-loaded, meaning values are produced on-the-fly reducing memory usage.
Using numpy.arange()
NumPy
library provides the arange()
function which supports ranges with non-integer steps. This is particularly useful for scientific and mathematical applications.
Python
import numpy as np
start = 0.5
stop = 5.0
step = 0.5
# Iterate through the range using NumPy's arange function
for i in np.arange(start, stop, step):
print(i, end=" ")
Output0.5 1.0 1.5 2.0 2.5 3.0 3.5 4.0 4.5
Explanation:
numpy.arange(start, stop, step)
generates numbers starting from start
to stop - step
, incremented by step
.- This method supports floating-point numbers as well.
- The loop iterates through the sequence generated by
arange()
.
itertools.count()
function generates numbers indefinitely, making it suitable for open-ended loops. However, such loops must include a condition to terminate execution.
Python
from itertools import count
start = 3
step = 2
for i in count(start, step):
if i > 15: # Termination condition
break
print(i, end=" ")
Explanation:
itertools.count(start, step)
produces an infinite sequence of numbers starting from start
and incremented by step
.- The loop includes a
break
statement to terminate execution once a condition is met.
Dynamic ranges are particularly useful when the parameters are determined at runtime. By collecting start
, stop
, and step
values from user input, we can define a loop range interactively.
Python
start = int(input("Enter the start of the range: "))
stop = int(input("Enter the end of the range: "))
step = int(input("Enter the step size: "))
for i in range(start, stop, step):
print(i, end=" ")
Output:
OutputExplanation:
- The
input()
function gathers user-defined values for start
, stop
, and step
, and these values are converted into integers using int()
. - These dynamically provided values define the loop range at runtime.
Using a list
Lists can serve as dynamic ranges, especially when the sequence of values isn’t linear or follows a specific pattern. This approach allows iteration over predefined or dynamically generated elements.
Python
l = [10, 20, 30, 40]
for i in l:
print(i, end=" ")
Explanation:
- A list
'l'
holds the elements to iterate over which can be predefined or generated dynamically. - The
for
loop iterates through the elements in the list.