3 from itertools import islice
4 from typing import Any, Iterator, List
7 def shard(lst: List[Any], size: int) -> Iterator[Any]:
8 """Yield successive size-sized shards from lst."""
9 for x in range(0, len(lst), size):
10 yield islice(lst, x, x + size)
13 def flatten(lst: List[Any]) -> List[Any]:
14 """Flatten out a list:
16 >>> flatten([ 1, [2, 3, 4, [5], 6], 7, [8, [9]]])
17 [1, 2, 3, 4, 5, 6, 7, 8, 9]
21 if isinstance(lst[0], list):
22 return flatten(lst[0]) + flatten(lst[1:])
23 return lst[:1] + flatten(lst[1:])
26 def prepend(item: Any, lst: List[Any]) -> List[Any]:
27 """Prepend an item to a list."""
28 lst = list.insert(0, item)