9

According to the Python documentation, when I do range(0, 10) the output of this function is a list from 0 to 9 i.e. [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]. However the Python installation on my PC is not outputting this, despite many examples of this working online.

Here is my test code...

test_range_function = range(0, 10)
print(test_range_function)
print(type(test_range_function))

The output of this I'm thinking should be the list printed, and the type function should output it as a list. Instead I'm getting the following output...

c:\Programming>python range.py
range(0, 10)
<class 'range'>

I haven't seen this in any of the examples online and would really appreciate some light being shed on this.

FiveAlive
  • 305
  • 1
  • 2
  • 4
  • 12
    Have you read python doc for 3.x version? To get a list, you have to do `list(range(0, 10))`. –  May 17 '13 at 22:34
  • 3
    This was a change from 2.x to 3, documented here: http://docs.python.org/3.0/whatsnew/3.0.html#views-and-iterators-instead-of-lists . Most of the time you're better off with an iterator anyway, but arbauthc's comment shows to create the list if you do need it. – Peter DeGlopper May 17 '13 at 22:36
  • 1
    If you're looking at the [Python documentation online](http://docs.python.org/3/index.html), always make sure you're reading the documentation for the version you're using. There's a little option menu in the upper-left corner of every page that shows, e.g., "2.7.5", which you can change to "3.3". – abarnert May 17 '13 at 23:14
  • Thanks for all the responses, I feel stupid not looking at the correct Python documentation but hopefully I won't make that mistake again! – FiveAlive May 17 '13 at 23:25
  • @FiveAlive: Well, _I_ still make that mistake even though I know better. (Why is this `print` function printing a tuple when I'm passing it two values, neither of which is a tuple?!) How else do you think we all recognized the problem so quickly? :) – abarnert May 18 '13 at 00:12

3 Answers3

19

That's because range and other functional-style methods, such as map, reduce, and filter, return iterators in Python 3. In Python 2 they returned lists.

What’s New In Python 3.0:

range() now behaves like xrange() used to behave, except it works with values of arbitrary size. The latter no longer exists.

To convert an iterator to a list you can use the list function:

>>> list(range(5)) #you can use list()
[0, 1, 2, 3, 4]
kgraney
  • 1,975
  • 16
  • 20
Ashwini Chaudhary
  • 244,495
  • 58
  • 464
  • 504
6

Usually you do not need to materialize a range into an actual list but just want to iterate over it. So especially for larger ranges using an iterator saves memory.

For this reason range() in Python 3 returns an iterator instead (as xrange() did in Python 2). Use list(range(..)) if you want an actual list instead for some reason.

ThiefMaster
  • 310,957
  • 84
  • 592
  • 636
  • 6
    +1, This explains the reason for the change and why it's better. One small, subtle distinction - `range()` in 3.x returns an iterable, not an iterator. It has some advantages over a straight up iterator, like it's not exhausted when it's looped over, and it's a sequence, so you can slice it, etc... `range(10)[:5] == range(0, 5)`. – Gareth Latty May 17 '13 at 22:47
  • 1
    … and it also implements an O(1) `__contains__`. so when novices write `5 in range(1, 21, 2)` (as they always have), it's no longer a mistake, but instance good and perfectly idiomatic code. – abarnert May 17 '13 at 23:15
1

range() does not return an iterator, it is not iterator it is iterable. iterators protocol has 2 methods. __iter__ and __next__

 r=range(10) 
  '__ iter __' in dir(r) # True
  `__ next __` in dir(r) # False

iterable protocol requires __iter__ which returns an iterator.

r.__iter__()
# <range_iterator object at 0x7fae5865e030>

range() uses lazy eveluation. That means it does not precalculate and store range(10). its iterator, range_iterator, computes and returns elements one at a time. This is why when we print a range object we do not actually see the contents of the range because they don't exist yet!.

Yilmaz
  • 35,338
  • 10
  • 157
  • 202
  • See also: What are iterator, iterable, and iteration? [https://stackoverflow.com/questions/9884132/what-are-iterator-iterable-and-iteration] (https://stackoverflow.com/questions/9884132/what-are-iterator-iterable-and-iteration) – Android Control Apr 19 '23 at 17:41
  • See also: Iterable: [https://docs.python.org/dev/glossary.html#term-iterable](https://docs.python.org/dev/glossary.html#term-iterable) – Android Control Apr 19 '23 at 17:49