How to use range(len()) in Python?

5/5 - (6 votes)

Problem Formulation

Have you come across the usage of range(len()) while trying to iterate across all the items of a given iterable?

Now, this brings up a couple of questions – (i) Why do we use range(len())? (ii) How do we use range(len())?


Generally, range(len()) allows you to iterate across a given iterable/sequence to access each item of the sequence with the help of its index.

Now, let’s visualize the answer to this to understand how this can be implemented in the code:

li = ['a', 'b', 'c']
for i in range(len(li)):



Explanation: In the above solution len(li) is used to find the length of the given list. Now, when you apply the range function upon len(li) as range(len(li)) it creates a squence of numbers from 0 up to len(li)-1. These numbers can then be used as indices of the given list such that you can access each item in the list using its index with the help of a for loop.


  • Python’s built-in function len() returns the length of the given string, array, list, tuple, dictionary, or any other iterable. The type of the returned value is an integer that represents the number of elements in this iterable. Learn more about the len() function here: Python len()
  • The Python range() function creates an iterable of subsequent integers within a given range of values. You can pass either only a stop argument in which case the range object will include all integers from 0 to stop (excluded). Or you can pass startstop, and step arguments in which case the range object will go from start to step using the given step size. For example, range(3) results in 0, 1, 2 and range(2, 7, 2) results in 2, 4, 6. Learn more about the len() function here: Python range() Function β€” A Helpful Illustrated Guide

❓Frequently Asked Question

Question: Is there a need for range(len(li))?

If you want to simply iterate across a list you can probably use a simple for loop to iterate across each item of the list one by one like so: “for x in list”. This is a better and more Pythonic way of accessing the elements from the list directly instead of using their index. But, there’s a catch!

Have a look at the following piece of code that filters out the numbers from a list which are less than both the left and the right neighbors.

li = [100, 78, 98, 62, 54, 36, 145]
res = []
for i in range(len(li)-2):
    left_node = li[i]
    current_node = li[i + 1]
    right_node = li[i+2]
    if current_node < left_node and current_node < right_node:
# [78, 36]

It wouldn’t be possible to solve this problem if you simply access the elements of the list. Here, you need the help of the indices of the each item in the list, to compute the index of an element, the index of the next element and the index of the previous element. So, this is where range(len()) comes in handy to solve the problem. Thus, it would be safe to say that the usage of range(len()) depends on the requirement of the code.


Before we wrap up this discussion, let’s dive into an interesting question to get a better grip on range(len()).

Question: Formulate an alternate solution to the given code snippet:

k = ['apple', 'mango', 'banana']
for i in range(len(k)):
    print("index: ", i)
    print("value: ", k[i])

Expected Output:

index:  0
value:  apple
index:  1
value:  mango
index:  2
value:  banana

Solution 1: Simply use a for loop to iterate across all the items in the given list and print each item one by one. To get the index of a particular object, you can use the index() method by calling it as: k.index(i) where k is the given list and i corresponds to each object of the given list.

k = ['apple', 'mango', 'banana']
for i in k:
    print("index: ", k.index(i))
    print("value: ", i)

Trivia: The list.index(value) method returns the index of the value argument in the list. You can use optional start and stop arguments to limit the index range where to search for the value in the list. If the value is not in the list, the method throws a ValueError.

Read more here: Python List index() – A Simple Illustrated Guide

Solution 2: Another way to approach this would be to leverage the power of the enumerate method in Python. Call the enumerate method and pass the given list within it. This allows you to generate a tuple that contains the index and its corresponding value in the list.

k = ['apple', 'mango', 'banana']
for i, val in enumerate(k):
    print("index: ", i)
    print("value: ", val)


Python’s built-in enumerate(iterable) function allows you to loop over all elements in an iterable and their associated counters. Formally, it takes an iterable as an input argument and returns an iterable of tuples(i, x)β€”one per iterable element x. The first integer tuple value is the counter of the element x in the iterable, starting to count from 0. The second tuple value is a reference to the element x itself. For example, enumerate(['a', 'b', 'c']) returns an iterable (0, 'a'), (1, 'b'), (2, 'c'). You can modify the default start index of the counter by setting the optional second integer argument enumerate(iterable, start).

Read more here: Python enumerate() β€” A Simple Illustrated Guide with Video


That brings us to the end of this discussion where we learned how to use the range and len functions together. We also learned the importance of using range(len()) and then we saw a few alternatives to it.

Please subscribe and stay tuned for more interesting discussions and tutorials. Happy coding!

For any software developer it is crucial to master the IDE well, to write, test and debug high-quality code with little effort. Do you want to master all these in the most popular Python IDE fast?

This course will take you from beginner to expert in PyCharm in ~90 minutes.

Join the PyCharm Masterclass now, and master PyCharm by tomorrow!