DEV Community

Rudolf Olah
Rudolf Olah

Posted on • Edited on

Python: no more need for range(len(...))

I was practising my algorithm writing skills on CodeSignal and one of the problems is how to sum up a matrix where there are certain conditions placed on the columns and rows.

What's neat about CodeSignal is that you can view and up-vote or down-vote other people's solutions after you have submitted your solution. Even in small algorithm problems, you can see how others are making use of clever tricks and libraries within the programming language used.

For instance, I used the Python enumerate function to traverse the rows and columns of the matrix:

for i, row in enumerate(matrix):
    for j, col in enumerate(row):
        print('matrix[{}][{}] = {}'.format(i, j, col))
Enter fullscreen mode Exit fullscreen mode

I noticed that other solutions to the problem were using this pattern:

for i in range(len(matrix)):
    for j in range(len(matrix[i])):
        print('matrix[{}][{}] = {}'.format(i, j, matrix[i][j]))
Enter fullscreen mode Exit fullscreen mode

This is something that makes sense in languages like C, C++ or JavaScript or even in Java. However, if there are iterators available that give you both the index and value, you should use them and match the idioms of the language.

Although, you do have to remember that when iterating over a slice of an array, you will need to set the starting index that enumerate starts at:

string = 'awesome'
for i, c in enumerate(string):
    print(i, c) # prints 0, a to 6, e
for i, c in enumerate(string[1:]):
    print(i, c) # prints 0, w to 5, e
for i, c in enumerate(string[1:], 1):
    print(i, c) # prints 1, w to 6, e
Enter fullscreen mode Exit fullscreen mode

On the flip side, I found a solution that was clever and used a combination of Python's itertools takewhile function, and the built-in sum and map functions.

There's a lot that all developers can learn, even in the smallest problems.

Top comments (2)

Collapse
 
viatrufka profile image
viatrufka

range(len()) is faster than enumerate. enumerate introduces additional overhead by creating an iterator and unpacking tuples.

Collapse
 
rudolfolah profile image
Rudolf Olah

Tried it out with an iterator over 1,000,000 numbers, my_list = list(range(1_000_000)):

Peak memory for enumerate(): 150.22 KiB
Peak memory for range(len()): 125.77 KiB
enumerate() took: 41.0845 seconds
range(len()) took: 40.6608 seconds
Enter fullscreen mode Exit fullscreen mode

So it's about a half second difference with an iterator, and peak memory diff was around 25 KiB.

I then tried it with a list of 1,000,000 dictionary objects, my_list = [{"id": index, "value": random.random()} for index in range(1_000_000)]:

Peak memory for enumerate(): 40.06 KiB
Peak memory for range(len()): 39.28 KiB
enumerate() took: 101.7232 seconds
range(len()) took: 101.4905 seconds
Enter fullscreen mode Exit fullscreen mode

I would be interested in seeing a benchmark of range and enumerate for a large number of objects in a production setting.