DEV Community

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

Posted on • Edited on

Iterator in Python (2)

Buy Me a Coffee

*Memos:

iter() or __iter__() can create an iterator with a list, tuple, set, dictionary, iterator, string or range() as shown below:

v1 = ['a', 'b', 'c', 'd', 'e'] # List
v2 = iter(v1)
v2 = v1.__iter__()

print(v2)
# <list_iterator object at 0x0000026906F3C460>

for x in v2:
    print(x)
# a
# b
# c
# d
# e
Enter fullscreen mode Exit fullscreen mode
v1 = ('a', 'b', 'c', 'd', 'e') # Tuple
v2 = iter(v1)
v2 = v1.__iter__()

print(v2)
# <tuple_iterator object at 0x000002821F7695A0>

for x in v2:
    print(x)
# a
# b
# c
# d
# e
Enter fullscreen mode Exit fullscreen mode
v1 = {'a', 'b', 'c', 'd', 'e'} # Set
v2 = iter(v1)
v2 = v1.__iter__()

print(v2)
# <set_iterator object at 0x00000282203069C0>

for x in v2:
    print(x)
# c
# a
# b
# d
# e
Enter fullscreen mode Exit fullscreen mode
v1 = {'fname': 'John', 'lname': 'Smith', 'age': 36, 'gender': 'Male'}
v2 = iter(v1)                                            # Dictionary
v2 = iter(v1.keys())
v2 = v1.__iter__()
v2 = v1.keys().__iter__()

print(v2)
# <dict_keyiterator object at 0x0000028220423FB0>

for x in v2:
    print(x)
# fname
# lname
# age
# gender

v2 = iter(v1.values())
v2 = v1.values().__iter__()

print(v2)
# <dict_valueiterator object at 0x00000282204FEE80>

for x in v2:
    print(x)
# John
# Smith
# 36
# Male

v2 = iter(v1.items())
v2 = v1.items().__iter__()

print(v2)
# <dict_itemiterator object at 0x00000282202F1670>

for x in v2:
    print(x)
# ('fname', 'John')
# ('lname', 'Smith')
# ('age', 36)
# ('gender', 'Male')
Enter fullscreen mode Exit fullscreen mode
v1 = 'Hello' # String
v2 = iter(v1)
v2 = v1.__iter__()

print(v2)
# <str_ascii_iterator object at 0x0000026906CEA560>

for x in v2:
    print(x)
# H
# e
# l
# l
# o
Enter fullscreen mode Exit fullscreen mode
v1 = range(5)
v2 = iter(v1)
v2 = v1.__iter__()

print(v2)
# <range_iterator object at 0x000001F954F52150>

for x in v2:
    print(x)
# 0
# 1
# 2
# 3
# 4
Enter fullscreen mode Exit fullscreen mode

A generator can create an iterator as shown below:

def func():
    yield 'a'
    yield 'b'
    yield from ['c', 'd', 'e']

print(func) # <function func at 0x000001FCD2E3CAE0>
print(type(func)) # <class 'function'>

gen = func()

print(gen) # <generator object func at 0x00000282207E3CC0>
print(type(gen)) # <class 'generator'>

for x in v2:
    print(x)
# a
# b
# c
# d
# e
Enter fullscreen mode Exit fullscreen mode

A generator comprehension can create a generator's iterator as shown below:

gen = (x.upper() for x in ['a', 'b', 'c', 'd', 'e'])

print(gen) # <generator object func at 0x00000282207E3CC0>
print(type(gen)) # <class 'generator'>

for x in gen:
    print(x)
# A
# B
# C
# D
# E
Enter fullscreen mode Exit fullscreen mode

A huge iterator doesn't get MemoryError as shown below:

gen = (x for x in range(100000000))

print(next(gen)) # 0
print(next(gen)) # 1
print(next(gen)) # 2
# ...
Enter fullscreen mode Exit fullscreen mode

itertools(repeat(), pairwise() and combinations()) can create iterators as shown below:

from itertools import repeat

v = repeat(object='Hello', times=3)

print(v)
# repeat('Hello')

print(type(v))
# <class 'itertools.repeat'>

print(next(v)) # Hello
print(next(v)) # Hello
print(next(v)) # Hello
print(next(v)) # StopIteration:
Enter fullscreen mode Exit fullscreen mode
from itertools import pairwise

v = pairwise('ABCD')
v = pairwise(['A', 'B', 'C', 'D'])

print(v)
# <itertools.pairwise object at 0x000001BE9A1ABF70>

print(next(v)) # ('A', 'B')
print(next(v)) # ('B', 'C')
print(next(v)) # ('C', 'D')
print(next(v)) # StopIteration:
Enter fullscreen mode Exit fullscreen mode
from itertools import combinations

v = combinations(iterable='ABC', r=2)

print(v)
# <itertools.combinations object at 0x000002690700D170>

print(next(v)) # ('A', 'B')
print(next(v)) # ('A', 'C')
print(next(v)) # ('B', 'C')
print(next(v)) # StopIteration:
Enter fullscreen mode Exit fullscreen mode

An iterator can be continuously used through multiple variables as shown below:

v1 = v2 = v3 = iter(['a', 'b', 'c', 'd', 'e']) # Equivalent
                                   # v1 = iter(['a', 'b', 'c', 'd', 'e'])
print(v1)                          # v2 = v1
print(v2)                          # v3 = v2
print(v3)
# <list_iterator object at 0x000002821F75D240>

print(next(v1)) # a
print(next(v2)) # b
print(next(v3)) # c
print(next(v1)) # d
print(next(v2)) # e
print(next(v3)) # StopIteration:
Enter fullscreen mode Exit fullscreen mode

An iterator except the one created by a generator or generator comprehension can be copied as shown below:

*Memos:

  • copy() does shallow copy. *There are no arguments.
  • deepcopy() does deep copy. *There are no arguments.
  • iter() cannot do shallow copy.
  • deepcopy() should be used because it's safe, doing copy deeply while copy() isn't safe, doing copy shallowly.
from copy import copy
from copy import deepcopy

v1 = iter(['a', 'b', 'c', 'd', 'e'])
v2 = copy(v1)
v2 = deepcopy(v1)

print(v1) # <list_iterator object at 0x00000200F45D19C0>
print(v2) # <list_iterator object at 0x000002821F75D240>

print(next(v1)) # a
print(next(v2)) # a
print(next(v1)) # b
print(next(v2)) # b
print(next(v1)) # c
print(next(v2)) # c
print(next(v1)) # d
print(next(v2)) # d
print(next(v1)) # e
print(next(v2)) # e
print(next(v1)) # StopIteration:
print(next(v2)) # StopIteration:
Enter fullscreen mode Exit fullscreen mode
from copy import copy
from copy import deepcopy

def func():
    yield 'a'
    yield 'b'
    yield from ['c', 'd', 'e']

gen1 = func()

gen2 = copy(gen1)
gen2 = deepcopy(gen1)
# TypeError: cannot pickle 'generator' object
Enter fullscreen mode Exit fullscreen mode
from copy import copy
from copy import deepcopy

gen1 = (x.upper() for x in ['a', 'b', 'c', 'd', 'e'])

gen2 = copy(gen1)
gen2 = deepcopy(gen1)
# TypeError: cannot pickle 'generator' object
Enter fullscreen mode Exit fullscreen mode

The variables v1 and v2 refer to the same iterator unless copied as shown below. *is keyword can check if v1 and v2 refer to the same iterator:

from copy import copy
from copy import deepcopy

v1 = iter(['a', 'b', 'c', 'd', 'e'])

v2 = v1 # v2 refers to the same iterator as v1.

print(v1) # <list_iterator object at 0x000001FCD6B64DC0>
print(v2) # <list_iterator object at 0x000001FCD6B64DC0>
print(v1 is v2) # True

v2 = copy(v1)     # v2 refers the different iterator
v2 = deepcopy(v1) # from v1.

print(v1) # <list_iterator object at 0x000001FCD6B64DC0>
print(v2) # <list_iterator object at 0x000001FCD6B66020>
print(v1 is v2) # False
Enter fullscreen mode Exit fullscreen mode

Top comments (0)