Question regarding iter() function

Hello, I am trying to analyze a script and I found this row in it:

new_words = iter([(starting_word, [starting_word])])

I tried executing this row and it produces:

starting_word = “Word1”
new_words = iter([(starting_word, [starting_word])])
new_words
<list_iterator object at 0x000002E0F5A0A1F0>
next(new_words)
(‘Word1’, [‘Word1’])
next(new_words)
Traceback (most recent call last):
File “<pyshell#9>”, line 1, in
next(new_words)
StopIteration

I don’t understand what is this good for. It is a part of this piece of code: GitHub - frankh/shortbread
The mentioned command is on row 26.

Do you not understand the purpose of the source data
[(starting_word,[starting_word])] or do you not understand the
behaviour of iterators?

If the latter:

I’d say that this line:

 new_words = iter([(starting_word, [starting_word])])

is intended to produce an iterator for the source data (a list of
(word,list-of-words) pairs). There can be many reasons for this,
particularly if this gets handed to something which iterates and the
source data are not originally iterable (though a list is iterable).

Anyway, that step produces an iterator for use elsewhere.

The interactive code you cite illustrates the iterator protocol, which
is that calling next(it) on some iterator it either produces the
next value from the iterator or raises StopIteration to indicate
that the iterator is now empty. All iterators behave this way.

When you iterate over some using, say, a for-loop, the mechanism of the
for-loop handles this behaviour for you.

Cheers,
Cameron Simpson cs@cskk.id.au

I had a quick look at the code you cite. It looks like the author makes a new_words iterator, then replaces it with another iterator depending on what goes on, which presumably iterates through the problem solution space looking for matches, where new_words is an iterable producing potential matches to try at the current stage of the search.

Cheers,
Cameron

Thank you very much, Cameron!

Vaclav