Sobes.tech
Junior — Middle

Which structure uses more memory in Python — range or list?

sobes.tech AI

Answer from AI

In Python, the range structure uses significantly less memory compared to a list containing the same elements. This is because range is a lazy iterator that only stores the start value, end value, and step, not all the elements of the sequence.

For example:

r = range(1000000)  # uses little memory
lst = list(r)       # uses memory for a million elements

Thus, the list stores all elements in memory, while range only stores the generation parameters, making it more memory-efficient.