16

When you do something like this:

for i in range(5):
      print i

What does Python do? Does it first generate an array with [0,1,2,3,4] and then goes over each item printing it? Similar to:

for i in [0,1,2,3,4]:
  print i

Or does it print each number as it generates them? Something like:

Generate 0 assign 0 to i print i

Generate 1 -> assign 1 to i -> print i

Generate 2 -> assign 2 to i -> print i

Generate 3 -> assign 3 to i -> print i

Generate 4 -> assign 4 to i -> print i

Update

I have added the tag for Python 2.7. I didn't think my question was version specific, but it seems it is!

Right now I am working with Python 2.7 so my question refers to that. But I find very valuable the information comparing Python 2 range against Python 3 range.

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Dzyann
  • 4,570
  • 10
  • 51
  • 86
  • @TigerhawkT3 If I remember correctly, `range` in Python 3 is more complicated than just a standard `yield` iterator, so I wouldn't think it would be a duplicate of that question – Brien Jul 17 '15 at 20:05
  • It's effectively a generator that implements some math for `__contains__` and some special logic so you can iterate over it twice, though. – NightShadeQueen Jul 17 '15 at 20:07
  • are you wondering about python 3 or python 2.7? They have two very different behaviors. – emschorsch Jul 17 '15 at 20:07
  • Yeah, I saw the part about generators generating and missed the part where it's actually about `range` rather than generators. Python 2 range at that, apparently. – TigerhawkT3 Jul 17 '15 at 20:07
  • Anyway, yeah, in Python 2, the `range` function really does return a `list`. That's why it was changed so radically (basically rewritten) for Python 3. – TigerhawkT3 Jul 17 '15 at 20:09
  • @emschorsch - I am a total newbie to Python, just learning right now, and I was wondering about that. I didn't know there was a difference between Python 2 and 3 on that matter. – Dzyann Jul 17 '15 at 20:18
  • @NightShadeQueen - I was actually working on Python 2, I didn't think there was a difference between both, that is why I didn't add the Python 2 tag. Also I feel the explanation comparing both Python 2 and 3 are very useful for me to understand the whole picture. That is why I am not totally convinced the question you pointed to is a duplicate. Would you agree? – Dzyann Jul 17 '15 at 20:23
  • The linked `range` question is indeed a duplicate, if you read the relevant answers. – TigerhawkT3 Jul 17 '15 at 20:26

2 Answers2

9

In Python 2, range() returns the whole list object then that list object is iterated by for.

In Python 3, it returns a memory efficient iterable, which is an object of its own with dedicated logic and methods, not a list. Now for will get each value as it is generated by that iterable.

In Python 2 there is xrange() to get something like what Python 3's range() do. Python 2's xrange is kind of halfway between Python 2's range and Python 3's range. It's better than the former but not as good as the latter.

Finally, in Python 3, if you want the whole list do:

>>> list(range(...))
jvdm
  • 794
  • 5
  • 18
0

It returns a list of the values. Try this to see

print range(4)
ate50eggs
  • 415
  • 2
  • 12
  • I know it returns a list of values, I just was wondering in combination with for how does it work. – Dzyann Jul 17 '15 at 20:19
  • No function should behave in a special way when it is used in different contexts, so `range` will always return a list in Python 2. – Matthias Jul 17 '15 at 20:29