Convert Range to List in Python (3 Examples)

 

Hi! This tutorial will show you how to turn ranges to lists in the Python programming language.

Here is an overview:

Let’s dive into the Python code!

 

Create Example Range of Integers

Here, we will use the range() function to create the example range of integer values that will be transformed into a list in this tutorial.

So, in your Python programming IDE, run the line of code below to create the example range of integer values:

my_range = range(1,10)
 
print(my_range)
 
# range(1, 10)
 
print(type(my_range))
 
# <class 'range'>

With the example range created, let us now see 3 ways it can be turned into a list.
 

Example 1: Turn Range to List Using list() Function

In this first example, we will transform the range object to a list object using the list() function:

my_list = list(my_range)
 
print(my_list)
 
# [1, 2, 3, 4, 5, 6, 7, 8, 9]
 
print(type(my_list))
 
# <class 'list'>

In the above example, we created a range object using the range() function and then parsed that range object as an argument to the list() function.

The list() function takes an iterable as input and returns a list containing the elements of that iterable. In this case, the range object is the iterable, and we get a list containing the same elements as the original range object.
 

Example 2: Turn Range to List Using Splat Unpack Operator

In this next example, we will use Python’s splat unpack operator to change the range to a list:

my_list = [*my_range]
 
print(my_list)
 
# [1, 2, 3, 4, 5, 6, 7, 8, 9]
 
print(type(my_list))
 
# <class 'list'>

Here, we use the splat operator * to unpack the range object and parse its elements as individual arguments to a new list. The splat operator is used to unpack any iterable into individual elements.

In this case, we use the splat operator to unpack the range object into a new list containing the same elements as the original range object.
 

Example 3: Turn Range to List Using List Comprehension

In this final example, we will use list comprehension to convert the range to a list:

my_list = [i for i in my_range]
 
print(my_list)
 
# [1, 2, 3, 4, 5, 6, 7, 8, 9]
 
print(type(my_list))
 
# <class 'list'>

In the above example, we used list comprehension to turn the range object into a list. List comprehension is a concise way to create a list in Python, and it works by iterating over an iterable and applying a transformation to each element.

In this case, we iterate over the range object using for i in my_range, and then add each element to a new list using the syntax [i for i in my_range]. The resulting list will contain the same elements as the original range object.

So, with that, we have demonstrated, with 3 simple examples, how to convert a range to a list in Python. I hope you found this tutorial helpful!
 

Video, Further Resources & Summary

Do you need more explanations on how to convert a range to a list in Python? Then you should have a look at the following YouTube video of the Statistics Globe YouTube channel.

In the video, we explain in some more detail how to convert a range to a list in Python.

 

The YouTube video will be added soon.

 

Furthermore, I encourage you to check out other interesting Python list tutorials on Statistics Globe, starting with these ones:

This post has shown how to turn a range object into a list in Python. In case you have further questions, you may leave a comment below.

 

R & Python Expert Ifeanyi Idiaye

This page was created in collaboration with Ifeanyi Idiaye. You might check out Ifeanyi’s personal author page to read more about his academic background and the other articles he has written for the Statistics Globe website.

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Top