More Itertools¶
Description:
- Additional building blocks, recipes, and routines for working with Python iterables.
Resources:
Example 1¶
Flatten a list of tuples
In [ ]:
Copied!
from more_itertools import flatten
iterable = [(0, 1), (2, 3)]
list(flatten(iterable))
from more_itertools import flatten
iterable = [(0, 1), (2, 3)]
list(flatten(iterable))
Example 2¶
Chunk a list into a list of lists
In [ ]:
Copied!
from more_itertools import chunked
iterable = [0, 1, 2, 3, 4, 5, 6, 7, 8]
list(chunked(iterable, 3))
from more_itertools import chunked
iterable = [0, 1, 2, 3, 4, 5, 6, 7, 8]
list(chunked(iterable, 3))
Example 3¶
“Look ahead” at items in the iterable without advancing it
In [ ]:
Copied!
from more_itertools import spy
iterable = (x * x for x in range(1, 6))
head, iterable = spy(iterable, n=3)
list(head)
list(iterable)
from more_itertools import spy
iterable = (x * x for x in range(1, 6))
head, iterable = spy(iterable, n=3)
list(head)
list(iterable)