DEV Community

Super Kai (Kazuya Ito)
Super Kai (Kazuya Ito)

Posted on

Iterator in Python (3)

Buy Me a Coffee

*Memo:

An iterator cannot be enlarged with * and a number as shown below:

v = iter([0, 1, 2, 3, 4]) * 3
# TypeError: unsupported operand type(s) for *: 'list_iterator' and 'int'
Enter fullscreen mode Exit fullscreen mode

An iterator and other iterators cannot be concatenated with + as shown below:

v = iter([0, 1, 2]) + iter([3, 4]) + iter([5, 6, 7, 8])
# TypeError: unsupported operand type(s) for +: 'list_iterator' and
# 'list_iterator'
Enter fullscreen mode Exit fullscreen mode

An iterator and other iterators cannot return:

  • all the elements in them with '|' (Union: A ∪ B).
  • their common elements with '&' (Intersection: A ∩ B).
  • the elements in the iterator which aren't in other iterators with '-' (Difference: A - B).
print(iter([0, 4]) | iter([0, 2, 4]) | iter([0, 1, 3, 4]))
# TypeError: unsupported operand type(s) for |: 'list_iterator' and
# 'list_iterator'
Enter fullscreen mode Exit fullscreen mode
print(iter([0, 4]) & iter([0, 2, 4]) & iter([0, 1, 3, 4]))
# TypeError: unsupported operand type(s) for &: 'list_iterator' and 
# 'list_iterator'
Enter fullscreen mode Exit fullscreen mode
print(iter([0, 4]) - iter([0, 2, 4]) - iter([0, 1, 3, 4]))
# TypeError: unsupported operand type(s) for -: 'list_iterator' and 
# 'list_iterator'
Enter fullscreen mode Exit fullscreen mode

An iterator and other iterator cannot return the elements in the iterator but not in other iterator or not in the iterator but in other iterator with '^' (Symmetric Difference: A Δ B) as shown below:

print(iter([0, 1, 2, 3]) ^ iter([0, 2, 4]))
# TypeError: unsupported operand type(s) for ^: 'list_iterator' and 
# 'list_iterator'
Enter fullscreen mode Exit fullscreen mode

An iterator can be iterated with a for statement as shown below:

<1D iterator>:

for x in iter([0, 1, 2, 3, 4]):
    print(x)
# 0
# 1
# 2
# 3
# 4
Enter fullscreen mode Exit fullscreen mode

<2D iterator>:

for x in iter([iter([0, 1, 2, 3]), iter([4, 5, 6, 7])]):
    for y in x:
        print(y)
# 0
# 1
# 2
# 3
# 4
# 5
# 6
# 7
Enter fullscreen mode Exit fullscreen mode

<3D iterator>:

for x in iter([iter([iter([0, 1]), iter([2, 3])]),
               iter([iter([4, 5]), iter([6, 7])])]):
    for y in x:
        for z in y:
            print(z)
# 0
# 1
# 2
# 3
# 4
# 5
# 6
# 7
Enter fullscreen mode Exit fullscreen mode

An iterator can be unpacked with an assignment and for statement, function and * but not with ** as shown below:

v1, v2, v3 = iter([0, 1, 2])

print(v1, v2, v3)
# 0 1 2
Enter fullscreen mode Exit fullscreen mode
v1, *v2, v3 = iter([0, 1, 2, 3, 4, 5])

print(v1, v2, v3)  # 0 [1, 2, 3, 4] 5
print(v1, *v2, v3) # 0 1 2 3 4 5
Enter fullscreen mode Exit fullscreen mode
for v1, v2, v3 in iter([iter([0, 1, 2]), iter([3, 4, 5])]):
    print(v1, v2, v3)
# 0 1 2
# 3 4 5
Enter fullscreen mode Exit fullscreen mode
for v1, *v2, v3 in iter([iter([0, 1, 2, 3, 4, 5]),
                         iter([6, 7, 8, 9, 10, 11])]):
    print(v1, v2, v3)
    print(v1, *v2, v3)
# 0 [1, 2, 3, 4] 5
# 0 1 2 3 4 5
# 6 [7, 8, 9, 10] 11
# 6 7 8 9 10 11
Enter fullscreen mode Exit fullscreen mode
print(*iter([0, 1]), 2, *iter([3, 4, *iter([5])]))
# 0 1 2 3 4 5
Enter fullscreen mode Exit fullscreen mode
print([*iter([0, 1]), 2, *iter([3, 4, *iter([5])])])
# [0, 1, 2, 3, 4, 5]
Enter fullscreen mode Exit fullscreen mode
def func(p1='a', p2='b', p3='c', p4='d', p5='e', p6='f'):
    print(p1, p2, p3, p4, p5, p6)

func()
# a b c d e f

func(*iter([0, 1, 2, 3]), *iter([4, 5]))
# 0 1 2 3 4 5
Enter fullscreen mode Exit fullscreen mode
def func(p1='a', p2='b', *args):
    print(p1, p2, args)
    print(p1, p2, *args)
    print(p1, p2, ['A', 'B', *args, 'C', 'D'])

func()
# a b ()
# a b
# a b ['A', 'B', 'C', 'D']

func(*iter([0, 1, 2, 3]), *iter([4, 5]))
# 0 1 (2, 3, 4, 5)
# 0 1 2 3 4 5
# 0 1 ['A', 'B', 2, 3, 4, 5, 'C', 'D']
Enter fullscreen mode Exit fullscreen mode

Top comments (0)