Python range() vs list(range()) Explained
range() and list(range()) can represent the same numbers, but they are not the same thing.
range()creates a range objectlist(range())creates a real list
This matters because beginners often expect range(5) to behave exactly like [0, 1, 2, 3, 4]. It does not. In most cases, you should use range() for looping, and only use list(range()) when you actually need a list.
Quick answer
numbers = range(5)
print(numbers) # range(0, 5)
print(list(numbers)) # [0, 1, 2, 3, 4]
Use range() for looping. Use list(range()) when you need an actual list value.
What is the difference?
The main difference is what each one returns.
range()returns a range object, not a listlist(range())converts that range object into a list- Both can represent the same sequence of numbers
- They are used differently in real programs
Example:
r = range(5)
lst = list(range(5))
print(r)
print(lst)
Output:
range(0, 5)
[0, 1, 2, 3, 4]
Even though they represent the same values, their types are different:
print(type(range(5)))
print(type(list(range(5))))
Output:
<class 'range'>
<class 'list'>
If you want to learn more about how range() works, see Python range() function explained.
What range() gives you
range() is most often used in for loops.
- It is commonly used in loops
- It represents numbers without storing them all as a list
- It is memory efficient for large sequences
- Printing it directly shows
range(start, stop[, step])
Example:
for i in range(5):
print(i)
Output:
0
1
2
3
4
Here, range(5) works perfectly in a loop. You do not need to convert it to a list first.
This is one reason range() is so common with Python for loops.
You can also inspect it:
r = range(2, 10, 2)
print(r)
print(len(r))
print(6 in r)
Output:
range(2, 10, 2)
4
True
What list(range()) gives you
list(range()) creates a full list in memory.
- It creates all values at once
- Printing it shows the actual numbers
- It is useful when you need list behavior
- It is less efficient than plain
range()for very large sequences
Example:
numbers = list(range(5))
print(numbers)
print(numbers[2])
Output:
[0, 1, 2, 3, 4]
2
This is helpful when you want clear output or when another part of your program expects a list.
If you want to understand the conversion step itself, see Python list() function explained.
When to use range()
Use range() when:
- You are writing a
forloop - You only need to iterate over numbers
- You are working with large sequences
- You do not need a real list
Example:
for i in range(1, 6):
print("Count:", i)
Output:
Count: 1
Count: 2
Count: 3
Count: 4
Count: 5
This is the most common use.
If you need both the index and the value while looping through another sequence, enumerate() is often a better choice than range(len(...)). See how to use enumerate() in Python.
When to use list(range())
Use list(range()) when:
- You need to see all values clearly
- Another part of your code expects a list
- You want to modify the result as a list
- You are learning and list output is easier to understand
Example:
numbers = list(range(5))
numbers.append(5)
print(numbers)
Output:
[0, 1, 2, 3, 4, 5]
This works because numbers is a list.
Important beginner note
range() can be used like a sequence, but it is still not a list.
That means:
- You can loop over it
- You can check its length with
len() - You can test membership with
in - You can index it
- But list methods such as
append()do not work on it
Example:
r = range(5)
print(r[2])
print(len(r))
print(3 in r)
Output:
2
5
True
Now compare that with this:
r = range(5)
r.append(5)
Output:
AttributeError: 'range' object has no attribute 'append'
If you need methods like append(), remove(), or sort(), convert the range to a list first.
If you run into this kind of problem, see how to fix AttributeError: object has no attribute.
Common confusion
A very common beginner mistake is expecting this:
print(range(5))
to show:
[0, 1, 2, 3, 4]
But in Python 3, it shows:
range(0, 5)
This does not mean the code is wrong.
It only means Python is showing the range object, not converting it to a list for display.
If you want list-style output, do this:
print(list(range(5)))
Output:
[0, 1, 2, 3, 4]
Common mistakes
These are the most common causes of confusion:
- Using
print(range(5))and expecting a list - Trying to call list methods on a range object
- Not realizing that
range()is iterable but not a list - Using
list(range())when plainrange()would be simpler
These quick checks can help:
print(range(5))
print(list(range(5)))
print(type(range(5)))
print(type(list(range(5))))
print(3 in range(5))
print(len(range(5)))
Output:
range(0, 5)
[0, 1, 2, 3, 4]
<class 'range'>
<class 'list'>
True
5
FAQ
Is range() a list in Python?
No. range() returns a range object. It can be iterated over, but it is not a list.
Why use range() instead of list(range())?
range() is better for loops and usually uses less memory. list(range()) creates a full list.
Can I index a range object?
Yes. A range object supports indexing, but it still is not a list.
Example:
r = range(5)
print(r[1])
Output:
1
Why does print(range(5)) not show all numbers?
Because Python shows the range object representation. Use list(range(5)) to display the numbers as a list.
When should I convert range() to a list?
Convert it when you need a real list, want list methods, or want clearer displayed output.