Про итераторы кратко

Самая важная схема занятия:

itertools
list(map(len, ['4', 'fdd']))
[1, 3]
1 in [1, 2, 3]
True
1 in {1, 2, 3}      # sets
True
1 in (1, 2, 3)      # tuples
True
d = {1: 'foo', 2: 'bar', 3: 'qux'}
1 in d
True
s = 'foobar'
'b' in s
True
for item in [1, 3, 4]:
  print(item)
1
3
4
lst = [1, 2, 3]
x = iter(lst)
print(type(x))
print(x)
<class 'list_iterator'>
<list_iterator object at 0x000001C1262DAC50>
next(x)
1
next(x)
2
next(x)
3
next(x)
---------------------------------------------------------------------------
StopIteration                             Traceback (most recent call last)
Cell In[28], line 1
----> 1 next(x)

StopIteration: 
from itertools import count
counter = count(start=13)
next(counter)
13
def repeater(value):
    while True:
        yield value
generator_obj = repeater('Эй')
next(generator_obj)
'Эй'
next(generator_obj)
'Эй'
numbers = [1, 2, 3, 4, 5, 6]
[x * x for x in numbers]
[1, 4, 9, 16, 25, 36]
lazy_squares = (x * x for x in numbers)
lazy_squares
<generator object <genexpr> at 0x7f671ce74cd0>
next(lazy_squares)
1
gen = (x * x for x in range(2328346283764826348726347628374628376482763482763482634876))
print(next(gen))
print(next(gen))
print(next(gen))
# отложенные вычисления, т.е. один элемент по запросу
0
1
4