What list range() function used?


#1

The range function has three different versions:

range(stop)
range(start, stop)
range(start, stop, step)

Where does the function take the original list?
If I have more than one list created in the program. Which list will the function choose?


#2

range produces a list:

print range(5) # outputs: [0, 1, 2, 3, 4]

so i am bit confused by your question, because it doesn’t take a list, it produces a list


#3

range() creates ONLY ONE list - [0, 1, 2, 3, 4, …], of different lengths?


#4

range() produces a single list, but the list produced depends on the arguments you supply.

range(0, 3) will generate a different list then range(4,9)


#5

Thank you! Now I realized that the function creates a definite arithmetic sequence, and does not allocate a part of the existing list.


#6

In Python 2, that is. In Python 3 it returns an iterator from which we can create a list.

list(range(5))

In Python 2 we still treat range() as an iterator in most usage cases, though from time to time we might wish to generate a sequence that can be accessed later.

range(start=>inclusive, stop=>exclusive, step=>with a direction)

threes = range(3, 100, 3)

produces,

[3, 6, 9, 12, .., 90, 93, 96, 99]