How to convert range to list in Python

Python

In this post, we are going to understand how to convert Range to a list in Python using multiple ways. Sometimes we need to create a list containing continuous values, for example, values from 10 to 1000. That can be achieved by using Python’s built-in range() function.

Ways to convert range to list in Python

  • Using List() constructor
  • Using list comprehesion
  • Using unpack operator
  • Using for loop with append() method
  • Using List extend() method
  • list extend() Exclusive range

1.List() to convert Range to list in Python


In the list() constructor we pass the range() function as an argument and it will return a list created from range.

In this example, We have passed the range(2,16,2) function that creates an integer number range sequence start from 2 to 16, increment by 1, and stop at 14. The list constructors return a list from the range of integer numbers sequence.

Program example

#python 3 program to convert range to list

range_to_list = list(range(2, 16, 2))
print(range_to_list)

Output

[2, 4, 6, 8, 10, 12, 14]

2. unpacking operator (*) to convert Range to list


We can also convert range() to list using the unpacking operator. To create a list from the range we pass the range() function to list literal([]) followed by an unpacking operator.

Program example

#Program to convert range to list python 3 

range_to_list = [*range(5)]
print(range_to_list)

Output

[0, 1, 2, 3, 4]

3. List comprehesion to convert Range


Another way to convert a range to a list is by using list compression. In this below example we are using List comprehension to create a list from range in Python.

Program example

[print(val,end=",") for val in range(1, 10)]

Output

1,2,3,4,5,6,7,8,9,

4. For loop with append() method to convert Range to list


We will iterate over the range of elements using for loop and append each element to the list using the list append() method.

Program Example

result_list = list()
for x in range(2, 10, 2) :
    result_list.append(x)
 
print(result_list)

Output

[2, 4, 6, 8]

5. list extend() method to convert Range to list in Python


The list extend() method can also be used to create a list from a range. In this example, we have passed the range() function to extend() method and it returning a list from the range.

Program Example

result_list = list()
start =2
stop =12
step= 2
if start<stop:
 result_list.extend(range(start,stop,step)) 
print(result_list)

Output

[2, 4, 6, 8, 10]

6. Exclusive range to convert Range to list


The range is exclusive in nature by default it does not include the last stop element in the range. If we want to add the stop element in range to the list we can achieve by workaround. Let us understand with an example.

In this example, we have extended the range to list using the extend() method and we wanted to append stop value so we have appended stop value using the list append() method.

Program Example include range stop value

result_list = list()
start =2
stop =12
step= 2
if start<stop:
 result_list.extend(range(start,stop,step))
 result_list.append(stop)
print(result_list)

Output

[2, 4, 6, 8, 10, 12]

Create a list of negative range


In this example, we will create a list of negative numbers from -1 to -10. Let us understand with the example we can use any of above mentioned ways to achieve this.

#start = -2 ,stop = -10 ,step = -2
result_list = list()
for x in range(-2, -10,-2) :
    result_list.append(x)
 
print(result_list)

Output

[-2, -4, -6, -8]

Summary

In this post, We have learned 6 different ways of How to convert the range to list in Python.The list constructor,list comprehension,for loop with append() ,list extend() and exclusive range to list using list extend() method.