Mohamed Msd
Mohamed Msd

Reputation: 37

When should I use iter function?

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.

Upvotes: 2

Views: 563

Answers (3)

juanpa.arrivillaga
juanpa.arrivillaga

Reputation: 95957

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]

Upvotes: 1

chepner
chepner

Reputation: 531315

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

Upvotes: 4

Green Cloak Guy
Green Cloak Guy

Reputation: 24691

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.

Upvotes: 3

Related Questions