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 ▸

Give a segment of python code that will print the numbers 1 to 7, each one on a new line


How do I use format python code?


How do I define a function in Python?


Write a recursive function that takes any integer n and prints the nth Fibonacci number.