Python
Python 3
range
list conversion
programming tutorial

Python 3 turn range to a list

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

Introduction

In Python 3, range() returns a range object, not a list. If you need a real list, the idiomatic conversion is list(range(...)), but it is worth understanding when that conversion is useful and when it is just unnecessary memory allocation.

The Direct Conversion

The normal answer is short:

python
numbers = list(range(5))
print(numbers)

Output:

python
[0, 1, 2, 3, 4]

The same works with explicit start, stop, and step values:

python
values = list(range(2, 12, 2))
print(values)

That produces:

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

So if the question is literally "how do I turn a range into a list," that is the answer.

Why Python 3 Does Not Return a List Automatically

Python 2 returned a list from range(), but Python 3 changed the behavior to avoid creating large lists unnecessarily. A range object stores only the arithmetic pattern and computes values lazily when needed.

python
1r = range(1_000_000)
2print(type(r))
3print(r[0])
4print(r[-1])

That object can represent one million numbers without building a one-million-element list in memory.

When a List Is Actually Needed

Convert a range when you need list behavior, for example:

  • modifying elements
  • appending new items
  • passing the result to code that specifically expects a list
  • serializing or printing the complete materialized sequence
python
1items = list(range(4))
2items[0] = 99
3items.append(100)
4print(items)

You cannot do those operations directly on a range object because it is immutable and does not have list methods.

Things You Can Do Without Conversion

A lot of Python beginners convert too early. Many common operations already work on range directly.

Iteration works:

python
for i in range(3):
    print(i)

Membership tests work:

python
print(42 in range(100))
print(41 in range(0, 100, 2))

Indexing works:

python
r = range(10, 20)
print(r[2])
print(r[-1])

Slicing works too, and the result is another range object:

python
1r = range(0, 20)
2part = r[2:10:2]
3print(part)
4print(list(part))

Because of that, converting immediately is often just habit rather than necessity.

Descending Ranges and Other Variants

Negative steps are handled exactly the same way:

python
countdown = list(range(10, 0, -2))
print(countdown)

You can also reverse a range explicitly:

python
print(list(reversed(range(5))))

Both are fine. Use whichever expression makes the intention clearer.

List Comprehensions Are Different

Developers sometimes write a list comprehension where a direct list conversion would be simpler:

python
values = [x for x in range(5)]

That works, but it is doing more work than necessary if you are not transforming the values. Prefer:

python
values = list(range(5))

Use a comprehension only when you need to map or filter:

python
squares = [x * x for x in range(5)]

Common Pitfalls

  • Converting a huge range to a list can consume large amounts of memory for no real benefit.
  • Carrying over Python 2 assumptions leads to confusion because Python 3 range is not a list.
  • Using a list comprehension just to copy the range values is unnecessary when list(range(...)) is clearer.
  • Repeatedly converting inside a loop adds needless allocation cost.
  • Forgetting that many built-in functions such as sum, min, and max already accept a range directly leads to extra work.

Summary

  • In Python 3, use list(range(...)) to materialize a range as a list.
  • 'range() itself returns a compact iterable object, not a prebuilt list.'
  • Convert only when you actually need list semantics.
  • Iteration, indexing, slicing, and membership checks already work on range.
  • Avoid large or repeated conversions unless there is a clear reason to materialize the sequence.

Related reading
Course
Intermediate
27 lessons
15 hours
DSA Fundamentals

Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

All Rights Reserved.