2

This code

for i in range(100, -1, -1):
   print(i)

Is same as:

for i in iter(range(100, -1, -1)):
   print(i)

Which print's numbers from a list of 0 .. 100 numbers in descending order.

I know about sentinel attribute which stops once it reaches it, but besides that when should i consider using iter() function?

Thank you.

wjandrea
  • 28,235
  • 9
  • 60
  • 81
  • 1
    https://docs.python.org/3/library/functions.html#iter – Prune Feb 13 '20 at 21:37
  • 3
    Sometimes, you want multiple independent iterators over the same iterable object. A good example can be found in the `grouper` function in the [`itertools` documentation](https://docs.python.org/3/library/itertools.html#itertools-recipes). – chepner Feb 13 '20 at 21:39
  • 1
    @G.Anderson No, my question is not about range(), it is about iter() function, i can do that to a list too. i.e `["aa", "bb", "ccc"]` .. – Mohamed Msd Feb 13 '20 at 21:44

3 Answers3

4

Here's an example. Say I have a list of items, where an item count is followed by that number of items, over and over.

[1, 'some data', 3, 'some data', 'some data', 'some data', 2, ...]

I might like to iterate over this data using nested for loops: the outer loop getting the item count, and the inner loop getting the following n elements of the list. The two loops can share an explicit iterator:

from itertools import islice

my_data = [1, "a", 3, "b", "c", "d", 2, "e", "f"]
my_iter = iter(my_data)
for i, count in enumerate(my_iter):
    print(f"Round {i}")
    for item in islice(my_iter, count):
        print(f'  {item}')

This code produces the output

Round 0
  a
Round 1
  b
  c
  d
Round 2
  e
  f
chepner
  • 497,756
  • 71
  • 530
  • 681
3

Not for this. iter() is good for turning things that would be memory-hungry iterables into generators, or for turning non-generator iterables that already exist into objects you can call next() on.

But range() is already a generator (well, not really, but it behaves very similarly to one and has the same advantages), so there's no benefit in this case.

For that matter, for for loops in general, if your iterable already fully exists, there's no point in making an iter() out of it - the for keyword does that behind-the-scenes already.

Green Cloak Guy
  • 23,793
  • 4
  • 33
  • 53
  • 1
    Important note: Despite being lazily evaluated and iterable, [Range is NOT a generator](https://stackoverflow.com/questions/13092267/if-range-is-a-generator-in-python-3-3-why-can-i-not-call-next-on-a-range) – G. Anderson Feb 13 '20 at 21:43
  • 1
    What's a "non-generator iterable"? Something like `range` is an iterable, but not an iterator. `iter(range(...))` returns something of type `range_iterator`, which *is* (as you would expect) an iterator. – chepner Feb 13 '20 at 21:44
  • @chepner when I was thinking non-generator iterables I was thinking of iterable data structures, mostly - `list`s, `set`s, `dict`s, etc. for which the memory is already allocated. – Green Cloak Guy Feb 13 '20 at 21:45
  • 1
    `iter` does not turn an iterable into a generator. A generator is simply one type of iterables, a function that yields. `iter` turns an iterable into an iterator by calling its `__iter__` method. The only reason `iter` is unneeded in the OP's case is because `for` always creates an iterator from the given iterable itself, just like `iter` does, making `iter` redundant. – blhsing Feb 13 '20 at 22:09
  • "iter() is good for turning things that would be memory-hungry iterables into generators" That is wrong, `iter` returns an iterator from an iterable. Also: "but range() is already a generator (well, not really, but it behaves very similarly to one and has the same advantages" no, it doesn't behave like a generator. It doesn't have a `__next__` method, in particular. It is an *iterable* container, not an *iterator*. `iter` is for when you want an iterator, which may be use for, say, manually iterating over something, or for various other reasons. And you may want one out of a `range` – juanpa.arrivillaga Feb 14 '20 at 02:50
1

Any time you want an iterator. That is what iter is for, it returns an iterator for what you pass to it. So, consider the following generator, which takes advantage of the properties of an iterator, namely, it is single pass and exhaustible, and extracting an iterator from an iterator should return the iterator itself, instead of giving you a new one.

In [19]: import itertools

In [20]: def chunk_by_n(iterable, n):
    ...:     islice = itertools.islice
    ...:     iterator = iter(iterable)
    ...:     chunk = list(islice(iterator, n))
    ...:     while chunk:
    ...:         yield chunk
    ...:         chunk = list(islice(iterator, n))
    ...:

In [21]: iterable = range(100)

In [22]: chunks = chunk_by_n(iterable, 3)

In [23]: next(chunks)
Out[23]: [0, 1, 2]

In [24]: next(chunks)
Out[24]: [3, 4, 5]

In [25]: next(chunks)
Out[25]: [6, 7, 8]

Now, look what happens if we don't make an iterator out of the input:

In [26]: def chunk_by_n(iterable, n):
    ...:     islice = itertools.islice
    ...:     #iterator = iter(iterable)
    ...:     iterator = iterable
    ...:     chunk = list(islice(iterator, n))
    ...:     while chunk:
    ...:         yield chunk
    ...:         chunk = list(islice(iterator, n))
    ...:

In [27]: chunks = chunk_by_n(iterable, 3)

In [28]: next(chunks)
Out[28]: [0, 1, 2]

In [29]: next(chunks)
Out[29]: [0, 1, 2]

In [30]: next(chunks)
Out[30]: [0, 1, 2]
juanpa.arrivillaga
  • 88,713
  • 10
  • 131
  • 172