*Memo:
- My post explains itertools about count(), cycle() and repeat().
- My post explains itertools about accumulate(), batched(), chain() and chain.from_iterable().
- My post explains itertools about groupby() and islice().
- My post explains itertools about pairwise(), starmap(), tee() and zip_longest().
- My post explains itertools about product() and permutations().
- My post explains itertools about combinations() and combinations_with_replacement().
- My post explains an iterator (1).
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
.
- It selects elements from
from itertools import compress
v = compress(data='', selectors=[])
v = compress(data=[], selectors=[])
print(v)
# <itertools.compress object at 0x0000026905F8CF10>
print(next(v))
# StopIteration:
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:
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
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
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
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 fromiterable
. - Don't use
predicate=
.
- If it's
- The 2nd argument is
iterable
(Required-Type:Iterable):- Don't use
iterable=
.
- Don't use
from itertools import filterfalse
v = filterfalse(lambda: False, [])
print(v)
# <itertools.filterfalse object at 0x0000026906CE8910>
print(next(v))
# StopIteration:
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:
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:
from itertools import filterfalse
for x in filterfalse(lambda a: a < 2, [0, -1, 2, -3, 4]):
print(x)
# 2
# 4
from itertools import filterfalse
for x in filterfalse(None, [0, -1, False, 2, None, -3, '', 4, [], -5, (), 6]):
print(x)
# 0
# False
# None
# Nothing
# []
# ()
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=
.
- Don't use
- The 2nd argument is
iterable
(Required-Type:Iterable):- Don't use
iterable=
.
- Don't use
from itertools import takewhile
v = takewhile(lambda: True, [])
print(v)
# <itertools.takewhile object at 0x0000026906F68C40>
print(next(v))
# StopIteration:
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:
from itertools import takewhile
for x in takewhile(lambda a: a < 2, [0, -1, 2, -3, 4]):
print(x)
# 0
# -1
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=
.
- Don't use
- The 2nd argument is
iterable
(Required-Type:Iterable):- Don't use
iterable=
.
- Don't use
from itertools import dropwhile
v = dropwhile(lambda: False, [])
print(v)
# <itertools.dropwhile object at 0x0000026906F696C0>
print(next(v))
# StopIteration:
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:
from itertools import dropwhile
for x in dropwhile(lambda a: a < 2, [0, -1, 2, -3, 4]):
print(x)
# 2
# -3
# 4
Top comments (0)