Python 3 turn range to a list
Categories:
Converting Python range
Objects to Lists in Python 3

Learn the most efficient and Pythonic ways to convert a range
object into a list
in Python 3, understanding the underlying mechanics and performance implications.
In Python 3, the range()
function behaves differently than in Python 2. Instead of returning a list directly, it returns an immutable sequence type called a range
object. This object generates numbers on demand, making it memory-efficient, especially for large sequences. However, there are many scenarios where you might need an actual list
containing all the numbers generated by a range
object. This article will guide you through the common and most Pythonic methods to achieve this conversion.
Understanding the range
Object
Before diving into conversions, it's crucial to understand what a range
object is. It's not a list; rather, it's an iterable that produces numbers when iterated over. This lazy evaluation is a key optimization in Python 3. A range
object only stores the start
, stop
, and step
values, not all the numbers it represents. This makes it very lightweight.
graph TD A[range(10)] --> B{Is it a list?} B -->|No| C[range object] C --> D{Iterate over it} D --> E[Generates numbers on demand] B -->|Yes (Python 2)| F[List of numbers]
Conceptual flow of range()
behavior in Python 3 vs. Python 2
Method 1: Using the list()
Constructor
The most straightforward and Pythonic way to convert a range
object to a list
is by passing the range
object directly to the list()
constructor. This constructor takes an iterable and returns a new list containing all items from the iterable.
my_range = range(5)
print(f"Type of my_range: {type(my_range)}")
my_list = list(my_range)
print(f"Type of my_list: {type(my_list)}")
print(f"Content of my_list: {my_list}")
# Example with start, stop, step
another_range = range(2, 10, 2)
another_list = list(another_range)
print(f"Content of another_list: {another_list}")
Converting a range
object to a list
using the list()
constructor.
list()
constructor is optimized for this kind of conversion.Method 2: Using List Comprehension (Less Common for Direct Conversion)
While not as direct as list()
, you can also use a list comprehension to achieve the same result. This method iterates over the range
object and collects each item into a new list. It's more verbose but can be useful if you need to perform an operation on each element during the conversion.
my_range = range(5)
my_list_comprehension = [x for x in my_range]
print(f"Content of my_list_comprehension: {my_list_comprehension}")
# Example with an operation during conversion
squared_numbers = [x*x for x in range(3)]
print(f"Squared numbers: {squared_numbers}")
Converting a range
object to a list
using list comprehension.
list(range_object)
is more efficient and readable than list comprehension. List comprehensions shine when you need to transform elements as they are added to the new list.Performance Considerations
When converting a range
object to a list
, especially for very large ranges, memory usage becomes a significant factor. A range
object consumes minimal memory, regardless of the size of the sequence it represents. However, converting it to a list
means all elements are generated and stored in memory simultaneously. Be mindful of this when working with extremely large ranges.

Memory footprint difference between range
and list
for a large sequence.
range(10**9)
to a list will attempt to allocate memory for one billion integers, which will likely lead to a MemoryError
on most systems. Only convert to a list if you genuinely need all elements in memory at once.