DEV Community

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

Posted on • Edited on

itertools in Python (7)

Buy Me a Coffee

*Memo:

islice() can return the iterator which returns the elements selected from iterable one by one in the range [start, end) as shown below:

*Memo:

  • The 1st argument is iterable(Required-Type:Iterable):
    • Don't use iterable=.
  • The 2nd argument is start or stop(Required-Type:int/NoneType):
    • It's a start index(inclusive) or stop index(exclusive).
    • It must be 0 <= x.
    • If start is None, it's 0.
    • If stop is None, iterable is read to the end.
    • Don't use start= or stop=.
  • The 3rd argument is stop(Optional-Default:None-Type:int/NoneType):
    • It's a stop index(exclusive).
    • It must be 0 <= x.
    • If it's set and None, iterable is read to the end.
    • If it's not set and None, the 2nd argument stop is prioritized.
    • Don't use stop=.
  • The 4th argument is step(Optional-Default:None-Type:int/NoneType):
    • It's the interval of indices.
    • It must be 1 <= x.
    • If it's None, it's 1.
    • Don't use step=.
  • Only if two arguments are set, the 2nd argument is stop.
  • start and stop can only be unsigned indices(zero and positive indices).
  • Error doesn't occur even if [start, end) is out of the range [The 1st element index, The last element index].
from itertools import islice

v = islice([-4, -3, -2, -1, 0, 1, 2, 3, 4], 9)
v = islice([-4, -3, -2, -1, 0, 1, 2, 3, 4], 20)
v = islice([-4, -3, -2, -1, 0, 1, 2, 3, 4], None)
v = islice([-4, -3, -2, -1, 0, 1, 2, 3, 4], 0, 9)
v = islice([-4, -3, -2, -1, 0, 1, 2, 3, 4], 0, 20)
v = islice([-4, -3, -2, -1, 0, 1, 2, 3, 4], None, None)
v = islice([-4, -3, -2, -1, 0, 1, 2, 3, 4], 0, 9, 1)
v = islice([-4, -3, -2, -1, 0, 1, 2, 3, 4], 0, 20, 1)
v = islice([-4, -3, -2, -1, 0, 1, 2, 3, 4], None, None, None)

print(v)
# <itertools.islice object at 0x0000026906E7BD30>

print(next(v)) # -4
print(next(v)) # -3
print(next(v)) # -2
print(next(v)) # -1
print(next(v)) # 0
print(next(v)) # 1
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 islice

v = islice([], 0)

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

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

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

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

pairwise() can return the iterator which returns a successive overlapping tuple of the two elements of iterable one by one as shown below:

*Memo:

  • The 1st argument is iterable(Required-Type:Iterable):
    • Don't use iterable=.
from itertools import pairwise

v = pairwise('')
v = pairwise([])

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

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

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

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 pairwise

for x in pairwise('ABCD'):
# for x in pairwise(['A', 'B', 'C', 'D']):
    print(x)
# ('A', 'B')
# ('B', 'C')
# ('C', 'D')
Enter fullscreen mode Exit fullscreen mode

Top comments (0)