Convert range to list Python

Note: range() returns an immutable sequence of numbers that can be easily converted to lists, tuples, sets etc.


Syntax of range()

The range() function can take a maximum of three arguments:

range(start, stop, step)

The

range(start, stop, step)
0 and
range(start, stop, step)
1 parameters in range() are optional.

Now, let's see how range() works with different number of arguments.


Example 1: range() with Stop Argument

If we pass a single argument to range(), it means we are passing the

range(start, stop, step)
5 argument.

In this case, range() returns a sequence of numbers starting from 0 up to the number (but not including the number).

# numbers from 0 to 3 (4 is not included)
numbers = range(4)
print(list(numbers))    # [0, 1, 2, 3]

# if 0 or negative number is passed, we get an empty sequence
numbers = range(-4)
print(list(numbers))    # []

Example 2: range() with Start and Stop Arguments

If we pass two arguments to range(), it means we are passing

range(start, stop, step)
0 and
range(start, stop, step)
5 arguments.

In this case, range() returns a sequence of numbers starting from

range(start, stop, step)
0 (inclusive) up to
range(start, stop, step)
5 (exclusive).

# numbers from 2 to 4 (5 is not included)
numbers = range(2, 5)
print(list(numbers))    # [2, 3, 4]

# numbers from -2 to 3 (4 is not included)
numbers = range(-2, 4)    
print(list(numbers))    # [-2, -1, 0, 1, 2, 3]

# returns an empty sequence of numbers
numbers = range(4, 2) 
print(list(numbers))    # []

Example 3: range() with Start, Stop and Step Arguments

If we pass all three arguments,

  • the first argument is
    range(start, stop, step)
    0
  • the second argument is
    range(start, stop, step)
    5
  • the third argument is
    range(start, stop, step)
    1

The

range(start, stop, step)
1 argument specifies the incrementation between two numbers in the sequence.

# numbers from 2 to 10 with increment 3 between numbers
numbers = range(2, 10, 3)
print(list(numbers))    # [2, 5, 8]

# numbers from 4 to -1 with increment of -1
numbers = range(4, -1, -1)    
print(list(numbers))    # [4, 3, 2, 1, 0]

# numbers from 1 to 4 with increment of 1
# range(0, 5, 1) is equivalent to range(5)
numbers = range(0, 5, 1) 
print(list(numbers))    # [0, 1, 2, 3, 4]

Note: The default value of

range(start, stop, step)
0 is 0, and the default value of
range(start, stop, step)
1 is 1. That's why
# numbers from 0 to 3 (4 is not included)
numbers = range(4)
print(list(numbers))    # [0, 1, 2, 3]

# if 0 or negative number is passed, we get an empty sequence
numbers = range(-4)
print(list(numbers))    # []
9 is equivalent to
# numbers from 2 to 4 (5 is not included)
numbers = range(2, 5)
print(list(numbers))    # [2, 3, 4]

# numbers from -2 to 3 (4 is not included)
numbers = range(-2, 4)    
print(list(numbers))    # [-2, -1, 0, 1, 2, 3]

# returns an empty sequence of numbers
numbers = range(4, 2) 
print(list(numbers))    # []
0.


range() in for Loop

The range() function is commonly used in a for loop to iterate the loop a certain number of times. For example,

This is how you do it with generators in python2.x as well. Typically speaking, you probably don't need a list though since you can come by the value of my_list[i] more efficiently (i + 1), and if you just need to iterate over it, you can just fall back on range.

Also note that on python2.x, xrange is still indexable1. This means that range on python3.x also has the same property2

If you need to create a list from the range, you can use the list constructor. This constructor will convert a range into a list.

1

2

my_list = list(range(10))

print(my_list)

If you run the code, you will get 10 elements starting from 0 to 9.

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

For Python 2

The above code works both for Python 3 and 2. If you are using Python 2, you can also use this code to get the same result.

1

2

new_list = range(10)

print(new_list)

Using the for loop

The method I showed you is the simplest way to do it. If you want, you can also do it using a loop.

1

2

3

4

5

6

my_list = []

 

for i in range(10):

    my_list.append(i)

 

print(my_list)

The result is the same as before.

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

Unpacking argument

For efficiency reasons, Python 3 no longer creates a list when you use the range function.

Python 3 range is similar to xrange from Python 2 – it creates an iterable range object.

If you want Python 3 to create a list, you can force it, using the positional-expansion operator.

1

2

3

4

5

new_list = [range(10)]

old_list = [*range(10)]

 

print(new_list)

print(old_list)

Now, you have two results. In the first one, the result is not unpacked. In the second one, there is a standard list.