DEV Community

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

Posted on • Edited on

itertools in Python (3)

Buy Me a Coffee

*Memo:


itertools has the functions to create iterators.

*more-itertools has more functions by installing with pip install more-itertools.


compress() can return the iterator which returns the elements of data one by one if the corresponding elements of selectors are True as shown below:

*Memo:

  • The 1st argument is data(Required-Type:Iterable).
  • The 2nd argument is selectors(Required-Type:Iterable):
    • It selects elements from data.
from itertools import compress

v = compress(data='', selectors=[])
v = compress(data=[], selectors=[])

print(v)
# <itertools.compress object at 0x0000026905F8CF10>

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

v = compress(data='ABCDE', selectors=[1, 0, 1, 1, 0])
v = compress(data=['A', 'B', 'C', 'D', 'E'], selectors=[1, 0, 1, 1, 0])

print(next(v)) # A
print(next(v)) # C
print(next(v)) # D
print(next(v)) # StopIteration:
Enter fullscreen mode Exit fullscreen mode
from itertools import compress

for x in compress(data='ABCDE', selectors=[1, 0, 1, 1, 0]):
# for x in compress(data=['A', 'B', 'C', 'D', 'E'],
#                   selectors=[1, 0, 1, 1, 0]):
    print(x)
# A
# C
# D
Enter fullscreen mode Exit fullscreen mode
from itertools import compress

for x in compress(data='ABCDE', selectors=[1, 0, 1, 1, 0, 1, 1, 1]):
# for x in compress(data=['A', 'B', 'C', 'D', 'E'],
#                   selectors=[1, 0, 1, 1, 0, 1, 1, 1]):
    print(x)
# A
# C
# D
Enter fullscreen mode Exit fullscreen mode
from itertools import compress

for x in compress(data='ABCDE', selectors=[1, 0, 1]):
# for x in compress(data=['A', 'B', 'C', 'D', 'E'], selectors=[1, 0, 1]):
    print(x)
# A
# C
Enter fullscreen mode Exit fullscreen mode

filterfalse() can return the iterator which returns the elements of iterable one by one if predicate returns False as shown below:

*Memo:

  • The 1st argument is predicate(Required-Type:Callable or NoneType):
    • If it's None, False elements are returned from iterable.
    • Don't use predicate=.
  • The 2nd argument is iterable(Required-Type:Iterable):
    • Don't use iterable=.
from itertools import filterfalse

v = filterfalse(lambda: False, [])

print(v)
# <itertools.filterfalse object at 0x0000026906CE8910>

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

v = filterfalse(lambda a: a < 2, [0, -1, 2, -3, 4])

print(next(v)) # 2
print(next(v)) # 4
print(next(v)) # StopIteration:
Enter fullscreen mode Exit fullscreen mode
from itertools import filterfalse

v = filterfalse(None, [0, -1, False, 2, None, -3, '', 4, [], -5, (), 6])

print(next(v)) # 0
print(next(v)) # False
print(next(v)) # None
print(next(v)) # Nothing
print(next(v)) # []
print(next(v)) # ()
print(next(v)) # StopIteration:
Enter fullscreen mode Exit fullscreen mode
from itertools import filterfalse

for x in filterfalse(lambda a: a < 2, [0, -1, 2, -3, 4]):
    print(x)
# 2
# 4
Enter fullscreen mode Exit fullscreen mode
from itertools import filterfalse

for x in filterfalse(None, [0, -1, False, 2, None, -3, '', 4, [], -5, (), 6]):
    print(x)
# 0
# False
# None
# Nothing
# []
# ()
Enter fullscreen mode Exit fullscreen mode

takewhile() can return the iterator which takes(returns) the elements of iterable one by one while predicate returns True, then drops the rest of elements of iterable once predicate returns False as shown below:

*Memo:

  • The 1st argument is predicate(Required-Type:Callable):
    • Don't use predicate=.
  • The 2nd argument is iterable(Required-Type:Iterable):
    • Don't use iterable=.
from itertools import takewhile

v = takewhile(lambda: True, [])

print(v)
# <itertools.takewhile object at 0x0000026906F68C40>

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

v = takewhile(lambda a: a < 2, [0, -1, 2, -3, 4])

print(next(v)) # 0
print(next(v)) # -1
print(next(v)) # StopIteration:
Enter fullscreen mode Exit fullscreen mode
from itertools import takewhile

for x in takewhile(lambda a: a < 2, [0, -1, 2, -3, 4]):
    print(x)
# 0
# -1
Enter fullscreen mode Exit fullscreen mode

dropwhile() can return the iterator which drops the elements of iterable while predicate returns True, then returns the rest of elements of iterable one by one once predicate returns False as shown below:

*Memo:

  • The 1st argument is predicate(Required-Type:Callable):
    • Don't use predicate=.
  • The 2nd argument is iterable(Required-Type:Iterable):
    • Don't use iterable=.
from itertools import dropwhile

v = dropwhile(lambda: False, [])

print(v)
# <itertools.dropwhile object at 0x0000026906F696C0>

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

v = dropwhile(lambda a: a < 2, [0, -1, 2, -3, 4])

print(next(v)) # 2
print(next(v)) # -3
print(next(v)) # 4
print(next(v)) # StopIteration:
Enter fullscreen mode Exit fullscreen mode
from itertools import dropwhile

for x in dropwhile(lambda a: a < 2, [0, -1, 2, -3, 4]):
    print(x)
# 2
# -3
# 4
Enter fullscreen mode Exit fullscreen mode

Top comments (0)