In Python 3.x ,

range(0,3) returns a class of immutable iterable objects that lets you iterate over them, it does not produce lists, and they do not store all the elements in the range in memory, instead they produce the elements on the fly (as you are iterating over them) , whereas list(range(0,3)) produces a list (by iterating over all the elements and appending to the list internally) .

Example -

>>> range(0,3)
range(0, 3)
>>> list(range(0,3))
[0, 1, 2]

Ideally, if you only want to iterate over that range of values , range(0,3) would be faster than (list(range(0,3)) because the latter has the overhead of producing a list before you start iterating over it.

In Python 2.x , range(0,3) produces an list, instead we also had an xrange() function that has similar behavior of range() function from Python 3.x (xrange was renamed to range in Python 3.x)

For Python 3.5, From the documentation -

Range objects implement the collections.abc.Sequence ABC, and provide features such as containment tests, element index lookup, slicing and support for negative indices

So you can do things like -

>>> range(0,10)[5]
5
>>> range(0,10)[3:7]
range(3, 7)
>>> 5 in range(6,10)
False
>>> 7 in range(1,8)
True

And all of these are constant time operations , as can be seen from this test -

In [11]: %timeit a = xrange(0,1000000)[1000]
1000000 loops, best of 3: 342 ns per loop

In [12]: %timeit a = xrange(0,1000000)[10000]
1000000 loops, best of 3: 342 ns per loop

In [13]: %timeit a = xrange(0,1000000)[100000]
1000000 loops, best of 3: 342 ns per loop

In [14]: %timeit a = xrange(0,1000000)[999999]
1000000 loops, best of 3: 342 ns per loop

In [15]: %timeit a = xrange(0,10000000)[9999999]
1000000 loops, best of 3: 339 ns per loop

In [16]: %timeit a = xrange(0,1000000000000)[9999999999]
1000000 loops, best of 3: 341 ns per loop
Answer from Anand S Kumar on Stack Overflow
🌐
University at Buffalo
math.buffalo.edu › ~badzioch › MTH337 › PT › PT-lists.html
Lists, ranges and “for” loops — MTH 337
As the examples above illustrate in many situations we may need to iterate a for loop over a sequence of consecutive integers. It would be inconvenient having to manually type a list of integers to code such iteration. Instead we can use the range function that generates integers in a given range.
🌐
Sololearn
sololearn.com › en › Discuss › 3234042 › what-is-the-difference-between-range5-and-listrange5-in-python
What is the difference between `range(5)` and `list(range(5))` in Python? | Sololearn: Learn to code for FREE!
August 18, 2023 - It doesn't actually generate all the numbers at once. On the other hand, `list(range(5))` converts the range object into a list, so you can see the actual numbers generated by the range.
Discussions

Does range() return a list or a range object?
Why do we need to convert it to a list isn’t it already a list? (if not then what is it?) More on discuss.codecademy.com
🌐 discuss.codecademy.com
0
0
July 28, 2019
python - Does range() really create lists? - Stack Overflow
Furthermore, the only apparent ... to believe that labeling range as a lists is incorrect. ... Your last sentence is incorrect. range objects implement __getitem__. ... This question is not aware of the differences between Python 2 and 3, and is not a dupe of the one ... More on stackoverflow.com
🌐 stackoverflow.com
What's the difference between for i in list: vs for i in range(len(list)):
>>> l = [ 'a', 'b', 'c', 'X', 'Y', 'Z' ] >>> for i in l: ... print(i) ... a b c X Y Z >>> for i in range(len(l)): ... print(i) ... 0 1 2 3 4 5 range(x) just gives you an object that yields the integers from 0 to x - 1 when iterated. More on reddit.com
🌐 r/AskProgramming
11
0
July 3, 2021
python 3.x - Difference between [range(5)] and list(range(5)) - Stack Overflow
Release notes and bug fixes for beta.stackoverflow.com ... 0 Is there a difference between traversing a list in python with range keyword and by use of other variable? More on stackoverflow.com
🌐 stackoverflow.com
Top answer
1 of 6
52

In Python 3.x ,

range(0,3) returns a class of immutable iterable objects that lets you iterate over them, it does not produce lists, and they do not store all the elements in the range in memory, instead they produce the elements on the fly (as you are iterating over them) , whereas list(range(0,3)) produces a list (by iterating over all the elements and appending to the list internally) .

Example -

>>> range(0,3)
range(0, 3)
>>> list(range(0,3))
[0, 1, 2]

Ideally, if you only want to iterate over that range of values , range(0,3) would be faster than (list(range(0,3)) because the latter has the overhead of producing a list before you start iterating over it.

In Python 2.x , range(0,3) produces an list, instead we also had an xrange() function that has similar behavior of range() function from Python 3.x (xrange was renamed to range in Python 3.x)

For Python 3.5, From the documentation -

Range objects implement the collections.abc.Sequence ABC, and provide features such as containment tests, element index lookup, slicing and support for negative indices

So you can do things like -

>>> range(0,10)[5]
5
>>> range(0,10)[3:7]
range(3, 7)
>>> 5 in range(6,10)
False
>>> 7 in range(1,8)
True

And all of these are constant time operations , as can be seen from this test -

In [11]: %timeit a = xrange(0,1000000)[1000]
1000000 loops, best of 3: 342 ns per loop

In [12]: %timeit a = xrange(0,1000000)[10000]
1000000 loops, best of 3: 342 ns per loop

In [13]: %timeit a = xrange(0,1000000)[100000]
1000000 loops, best of 3: 342 ns per loop

In [14]: %timeit a = xrange(0,1000000)[999999]
1000000 loops, best of 3: 342 ns per loop

In [15]: %timeit a = xrange(0,10000000)[9999999]
1000000 loops, best of 3: 339 ns per loop

In [16]: %timeit a = xrange(0,1000000000000)[9999999999]
1000000 loops, best of 3: 341 ns per loop
2 of 6
13

It depends on what version of Python you are using.

In Python 2.x, range() returns a list, so they are equivalent.

In Python 3.x, range() returns an immutable sequence type, you need list(range(0,2)) to get a list.

🌐
MyTutor
mytutor.co.uk › answers › 59088 › Mentoring › Python › What-is-the-difference-between-using-range-and-a-list-of-values
What is the difference between using range() and a list of values?<!-- --> | MyTutor
The main differences between the two boil down to the difference between greedy and lazy evaluation. With a list, each indexed object takes up some space in memory. Range, however, calculates the next value on the fly, meaning only one object is stored in memory each time.
🌐
Python Examples
pythonexamples.org › python-range-vs-list-performance
Range vs List Performance
The following are the few key factors to consider the comparison between the performance of a range and a list. Ranges are more memory-efficient than lists. Ranges only store the start, stop, and step values, while the actual numbers are generated on-the-fly when iterated.
🌐
Stanford CS
cs.stanford.edu › people › nick › py › python-range.html
Python range() Function
This use of list() is only for printing, not needed to use range() in a loop.
Top answer
1 of 4
106

In Python 2.x, range returns a list, but in Python 3.x range returns an immutable sequence, of type range.

Python 2.x:

>>> type(range(10))
<type 'list'>
>>> type(xrange(10))
<type 'xrange'>

Python 3.x:

>>> type(range(10))
<class 'range'>

In Python 2.x, if you want to get an iterable object, like in Python 3.x, you can use xrange function, which returns an immutable sequence of type xrange.

Advantage of xrange over range in Python 2.x:

The advantage of xrange() over range() is minimal (since xrange() still has to create the values when asked for them) except when a very large range is used on a memory-starved machine or when all of the range’s elements are never used (such as when the loop is usually terminated with break).

Note:

Furthermore, the only apparent way to access the integers created by range() is to iterate through them,

Nope. Since range objects in Python 3 are immutable sequences, they support indexing as well. Quoting from the range function documentation,

Ranges implement all of the common sequence operations except concatenation and repetition

...

Range objects implement the collections.abc.Sequence ABC, and provide features such as containment tests, element index lookup, slicing and support for negative indices.

For example,

>>> range(10, 20)[5]
15
>>> range(10, 20)[2:5]
range(12, 15)
>>> list(range(10, 20)[2:5])
[12, 13, 14]
>>> list(range(10, 20, 2))
[10, 12, 14, 16, 18]
>>> 18 in range(10, 20)
True
>>> 100 in range(10, 20)
False

All these are possible with that immutable range sequence.


Recently, I faced a problem and I think it would be appropriate to include here. Consider this Python 3.x code

from itertools import islice
numbers = range(100)
items = list(islice(numbers, 10))
while items:
    items = list(islice(numbers, 10))
    print(items)

One would expect this code to print every ten numbers as a list, till 99. But, it would run infinitely. Can you reason why?

Solution

Because the range returns an immutable sequence, not an iterator object. So, whenever islice is done on a range object, it always starts from the beginning. Think of it as a drop-in replacement for an immutable list. Now the question comes, how will you fix it? Its simple, you just have to get an iterator out of it. Simply change

numbers = range(100)

to

numbers = iter(range(100))

Now, numbers is an iterator object and it remembers how long it has been iterated before. So, when the islice iterates it, it just starts from the place where it previously ended.

2 of 4
8

It depends.

In python-2.x, range actually creates a list (which is also a sequence) whereas xrange creates an xrange object that can be used to iterate through the values.

On the other hand, in python-3.x, range creates an iterable (or more specifically, a sequence)

Find elsewhere
🌐
Sololearn
sololearn.com › en › Discuss › 1467744 › differences-between-list-slicing-and-range-in-python
Differences between "List slicing" and "Range" in Python | Sololearn: Learn to code for FREE!
range returns a generator for numbers ... generate: [1,2,3,4,5,6,7,8,9,10] in other words, range is used to CREATE data while list slicing is used on an already existing list using the list from the example above mySlicedList = ...
🌐
GeeksforGeeks
geeksforgeeks.org › python › range-to-a-list-in-python
range() to a list in Python - GeeksforGeeks
July 11, 2025 - However, it produces a range object, which is an iterable but not a list. If we need to manipulate or access the numbers as a list, we must explicitly convert the range object into a list.
🌐
Quora
quora.com › What-is-the-difference-between-for-I-in-list-and-for-I-in-range-len-list
What is the difference between 'for I in list' and 'for I in range (len (list))'? - Quora
Answer (1 of 6): [code]>>> animals = ["cat", "dog", "fish"] >>> for animal in animals: ... print(animal) ... cat dog fish >>> animals ['cat', 'dog', 'fish'] >>> for index in range(len(animals)): ... print(index) ... 0 1 2 [/code]The first iterates over the elements in the list so the v...
🌐
Python Central
pythoncentral.io › pythons-range-function-explained
What Is the Range of the Function | Python for Range | Range() Python
January 27, 2022 - So in Python 3.x, the range() function got its own type. In basic terms, if you want to use range() in a for loop, then you're good to go. However you can't use it purely as a list object. For example you cannot slice a range type. When you're using an iterator, every loop of the for statement produces the next number on the fly. Whereas the original range() function produced all numbers instantaneously, before the for loop started executing.
🌐
Medium
medium.com › @sjalexandre › python-tutorial-unit-14-a2f5bd0564fd
Python Tutorial — Ranges, Sets, Tuples | Medium
August 12, 2023 - Lists are ordered, mutable, and allow duplicate elements. They are defined by having values between square brackets []. Sets are unordered, mutable, and do not allow duplicate elements.
🌐
AlgoMaster
algomaster.io › home › python › len & range
len & range | Python | AlgoMaster.io
June 6, 2026 - The interesting parts sit underneath: len calls a special method that any class can implement, and range isn't a list at all but a virtual sequence that supports indexing, slicing, and O(1) membership tests on huge ranges.
🌐
W3Schools
w3schools.com › python › python_range.asp
Python range
The step value means the difference between each number in the sequence. It is optional, and if not provided, it defaults to 1. range(3, 10, 2) returns a sequence of each number from 3 to 9, with a step of 2: ... Ranges are often used in for loops to iterate over a sequence of numbers. ... The range object is a data type that represents an immutable sequence of numbers, and it is not directly displayable. Therefore, ranges are often converted to lists for display.
🌐
Guru99
guru99.com › home › python › python range() function: float, list, for loop examples
Python range() Function: Float, List, For loop Examples
August 12, 2024 - You can see the output is a list format. It was not necessary to loop the range() and using list() method we could directly convert the output from range to list format. So far, we have used integers in python range(). We have also seen that floating-point numbers are not supported in python range.
🌐
CodeRivers
coderivers.org › blog › python-range-to-list
Python `range` to `list`: A Comprehensive Guide - CodeRivers
February 22, 2026 - Converting a range object to a list in Python is a straightforward process using the list() constructor. However, understanding the fundamental concepts, usage methods, common practices, and best practices is essential for writing efficient and effective code. By being aware of the differences between range and list in terms of memory usage and mutability, you can make informed decisions about when to use each data type.
🌐
Programiz
programiz.com › python-programming › methods › built-in › range
Python range() Function
The start and step arguments are optional. The range() function returns an immutable sequence of numbers. # create a sequence from 0 to 3 (4 is not included) numbers = range(4) # convert to list and print it print(list(numbers)) # Output: [0, 1, 2, 3]