What is the difference between using range() and a list of values?

The main differences between the two boil down to the difference between greedy and lazy evaluation. With a list, each indexed object takes up some space in memory. Range, however, calculates the next value on the fly, meaning only one object is stored in memory each time. However, this means range has to calculate each successive value, slowing down the processing speed.

LS

Related Python Mentoring answers

All answers ▸

Use recursion to print all the sublists of a given list


What can I do with Python?


Define a function that takes in the age of the user and adds it to an output sentence such as "You are ..... old".


Describe both For-loops and While-loops and explain how you can simulate the effect of a for loop with a while loop with an example.