Not found in the documentation

Matt Wheeler m at funkyhat.org
Thu Apr 29 13:36:55 EDT 2021


On Wed, 28 Apr 2021 at 22:18, Dennis Lee Bieber <wlfraed at ix.netcom.com> wrote:
<snip>
>         The old range() returned a list, and said list could (in your example)
> contain 42. The current range() (equivalent to former xrange() ) is not a
> container as retrieving values consumes them from the range.

A nitpick -- retrieving values from a range doesn't consume them:

```
>>> r = range(10)
>>> r
range(0, 10)
>>> list(r)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> list(r)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> r[0]
0
>>> r[3]
3
>>> list(r)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
```
range objects are iterables, not iterators.
We can see the consuming behaviour I think you are referring to by
calling iter():
```
>>> i = iter(r)
>>> next(i)
0
>>> list(i)
[1, 2, 3, 4, 5, 6, 7, 8, 9]
```


--
Matt Wheeler
http://funkyh.at


More information about the Python-list mailing list