3 from itertools import islice
4 from typing import Any, Iterator, List
7 def shard(lst: List[Any], size: int) -> Iterator[Any]:
9 Yield successive size-sized shards from lst.
11 >>> for sublist in shard([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12], 3):
12 ... [_ for _ in sublist]
19 for x in range(0, len(lst), size):
20 yield islice(lst, x, x + size)
23 def flatten(lst: List[Any]) -> List[Any]:
27 >>> flatten([ 1, [2, 3, 4, [5], 6], 7, [8, [9]]])
28 [1, 2, 3, 4, 5, 6, 7, 8, 9]
33 if isinstance(lst[0], list):
34 return flatten(lst[0]) + flatten(lst[1:])
35 return lst[:1] + flatten(lst[1:])
38 def prepend(item: Any, lst: List[Any]) -> List[Any]:
40 Prepend an item to a list.
42 >>> prepend('foo', ['bar', 'baz'])
50 if __name__ == '__main__':