DEV Community

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

Posted on • Edited on

Iterator in Python (2)

Buy Me a Coffee

*Memo:

An iterator can be used indirectly with len() after using list(), tuple(), set() and frozenset() and directly with more_itertools.ilen() to get the length as shown below:

*Memo:

  • more-itertools must be installed with pip install more-itertools.
  • An iterator cannot be directly used with len() to get the length.
from copy import copy
from more_itertools import ilen

v1 = iter([0, 1, 2, 3, 4])

v2 = copy(v1)
print(len(list(v2)))
# 5

v2 = copy(v1)
print(len(tuple(v2)))
# 5

v2 = copy(v1)
print(len(set(v2)))
# 5

v2 = copy(v1)
print(len(frozenset(v2)))
# 5

v2 = copy(v1)
print(ilen(v2))
# 5

v2 = copy(v1)
print(len(v2))
# TypeError: object of type 'list_iterator' has no len()
Enter fullscreen mode Exit fullscreen mode

A non-empty iterator and empty iterator are:

  • True, checking them with bool() respectively.
  • False, inverting their truth values with not respectively.
# Non-empty iterator
print(bool(iter([0])))
print(bool(iter([iter([])])))

# Empty iterator
print(bool(iter([])))
# True
Enter fullscreen mode Exit fullscreen mode
# Non-empty iterator
print(not iter([0]))
print(not iter([iter([])]))

# Empty iterator
print(not iter([]))
# False
Enter fullscreen mode Exit fullscreen mode

An iterator can be checked if a specific element is and isn't in the iterator with in and not in respectively as shown below:

v = iter(['A', iter(['B', 'C'])])

print('A' in v)
# True

print('B' in v)
print('C' in v)
print(['A'] in v)
print(['B'] in v)
print(['C'] in v)
print(['B', 'C'] in v)
print(['A', ['B', 'C']] in v)
print(iter(['A']) in v)
print(iter(['B']) in v)
print(iter(['C']) in v)
print(iter(['B', 'C']) in v)
print(iter(['A', iter(['B', 'C'])]) in v)
print(iter(['A', ['B', 'C']]) in v)
print(['A', iter(['B', 'C'])] in v)
# False
Enter fullscreen mode Exit fullscreen mode
v = iter(['A', iter(['B', 'C'])])

print('A' not in v)
# False

print('B' not in v)
print('C' not in v)
print(['A'] not in v)
print(['B'] not in v)
print(['C'] not in v)
print(['B', 'C'] not in v)
print(['A', ['B', 'C']] not in v)
print(iter(['A']) not in v)
print(iter(['B']) not in v)
print(iter(['C']) not in v)
print(iter(['B', 'C']) not in v)
print(iter(['A', iter(['B', 'C'])]) not in v)
print(iter(['A', ['B', 'C']]) not in v)
print(['A', iter(['B', 'C'])] not in v)
# True
Enter fullscreen mode Exit fullscreen mode

An iterator can be checked if the iterator is and isn't referred to by two variables with is and is not respectively as shown below:

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

print(v1 is v2) # False
print(v1 is v3) # True

print(v1 is not v2) # True
print(v1 is not v3) # False
Enter fullscreen mode Exit fullscreen mode

An iterator and other iterator cannot be checked if:

  • all the elements in them are and aren't the equal with == and != respectively.
  • the iterator is greater than other iterator with >.
  • the iterator is greater than or equal to other iterator with >=.
  • the iterator is less than other iterator with <.
  • the iterator is less than or equal to other iterator with <=.

*Memo:

  • == and != always return False and True respectively.
v = iter([0, 1, 2])

print(v == iter([0, 1, 2]))      # False
print(v == iter([2, 1, 0]))      # False
print(v == iter([0, 1, 3]))      # False
print(v == iter([0, 1, -3]))     # False
print(v == iter([0, 1]))         # False
print(v == iter([0, 3]))         # False
print(v == iter([0, -3]))        # False
print(v == iter([0, 1, 2, 3]))   # False
print(v == iter([0, 1, 3, 4]))   # False
print(v == iter([0, 1, -3, -4])) # False
print(v == iter([]))             # False
Enter fullscreen mode Exit fullscreen mode
v = iter([0, 1, 2])

print(v != iter([0, 1, 2]))      # True
print(v != iter([2, 1, 0]))      # True
print(v != iter([0, 1, 3]))      # True
print(v != iter([0, 1, -3]))     # True
print(v != iter([0, 1]))         # True
print(v != iter([0, 3]))         # True
print(v != iter([0, -3]))        # True
print(v != iter([0, 1, 2, 3]))   # True
print(v != iter([0, 1, 3, 4]))   # True
print(v != iter([0, 1, -3, -4])) # True
print(v != iter([]))             # True
Enter fullscreen mode Exit fullscreen mode
v = iter([0, 1, 2])

print(v > iter([0, 1, 2]))
# TypeError: '>' not supported between instances of 'list_iterator' and
# 'list_iterator'

print(v >= iter([0, 1, 2]))
# TypeError: '>=' not supported between instances of 'list_iterator' and
# 'list_iterator'

print(v < iter([0, 1, 2]))
# TypeError: '<' not supported between instances of 'list_iterator' and
# 'list_iterator'

print(v <= iter([0, 1, 2]))
# TypeError: '<=' not supported between instances of 'list_iterator' and
# 'list_iterator'
Enter fullscreen mode Exit fullscreen mode

An iterator and other iterator cannot be checked if they have and don't have their common elements with bool() and & and with not and & respectively as shown below:

v = iter([0, 1, 2])

print(bool(v & iter([1, 3])))
print(not (v & iter([1, 3])))
# TypeError: unsupported operand type(s) for &: 'list_iterator' and
# 'list_iterator'
Enter fullscreen mode Exit fullscreen mode

Top comments (0)