How do you find the length of a range object in Python?
In Python the built-in function
len()
can provide the length of a string or list, can the same function be used to count the length of a range object too?
What Is A Range Object?
A range object is one of the three basic sequence types in Python alongside tuples and lists.
The built-in function
range(start, stop[, step])
creates a range object and if you’re familiar with the slice operator then you’re familiar with a range object and the parameters used in this function.
Here’s an example comparing the
range()
function and the slice operator.
>>> my_range = range(1, 10, 1)
>>> print(*my_range)
1 2 3 4 5 6 7 8 9
As you can see from the above code where the asterisk operator is used to help expand the contents of the range object you see that the
range()
function produces a list of numbers from 1 to 9.
What Does
len(range)
Do?
To get the length of a range simply wrap the range object into the single parameter of the built-in
len()
function.
Here’s an example continuing on with the working example above:
>>> len(my_range)
9
The length of my original range is
9
and matches the result from the length function.
Length Of Range With Step Greater Than 1
The third parameter of the
range()
function is an optional parameter labelled
step
and acts in the same manner as the
step
parameter in the slice operator.
When the
step
parameter is greater than 1 this would produce a lower quantity of values in the range object as the jump from
start
to
stop
is done at larger jumps than 1.
With the simple example of the range object with numbers from 1 to 9 if a step value of 2 was used and the
len()
function used on the new range what would be the expected result?
Have a look at the example below:
>>> my_range = range(1, 9, 2)
>>> print(*my_range)
1 3 5 7
>>> len(my_range)
4
The
len()
function provides the correct items in the range function when the range is shortened due to a higher step.
What Does
range(len)
Do?
Another popular use case for using the
range()
function is when an iteration is needed according to the length of something, such as a list or dictionary.
Wrapping the
len()
function inside the
range()
function as its only parameter means the range object will start at
0
and progress to the value of the length of the item in its parameter.
Here’s an example demonstrating the use of this technique:
>>> my_list = [1, 2, 3, 4, 5]
>>> my_range = range(len(my_list))
>>> print(*my_list)
0 1 2 3 4
Notice the difference when the order of the two functions are changed! When the length of something is used as the sole parameter of the range function it produces a list of numbers from 0 to the length excluding the length value.
This use helps when iterating through an item when an index is needed.
Summary
The two functions
range()
and
len()
can be used in conjunction with one another but the order of how these two functions are used will produce different results.
Order is important for both.
range(len(x))
will produce an index list of numbers to the length of the item
x
but excluding that number.
len(range(start, stop, step)
will produce a number according to the range object contents and this can vary greatly, especially if the
step
value is used.