summaryrefslogtreecommitdiff
path: root/list_utils.py
diff options
context:
space:
mode:
authorScott Gasch <[email protected]>2021-03-24 18:08:54 -0700
committerScott Gasch <[email protected]>2021-03-24 18:08:54 -0700
commit497fb9e21f45ec08e1486abaee6dfa7b20b8a691 (patch)
tree47aa97a0fca36c4e7025cee5ad4e9ec6db49b881 /list_utils.py
Initial revision
Diffstat (limited to 'list_utils.py')
-rw-r--r--list_utils.py24
1 files changed, 24 insertions, 0 deletions
diff --git a/list_utils.py b/list_utils.py
new file mode 100644
index 0000000..9a5d4fd
--- /dev/null
+++ b/list_utils.py
@@ -0,0 +1,24 @@
+#!/usr/bin/env python3
+
+from itertools import islice
+from typing import Any, Iterator, List
+
+
+def shard(lst: List[Any], size: int) -> Iterator[Any]:
+ """Yield successive size-sized shards from lst."""
+ for x in range(0, len(lst), size):
+ yield islice(lst, x, x + size)
+
+
+def flatten(lst: List[Any]) -> List[Any]:
+ """Flatten out a list:
+
+ >>> flatten([ 1, [2, 3, 4, [5], 6], 7, [8, [9]]])
+ [1, 2, 3, 4, 5, 6, 7, 8, 9]
+
+ """
+ if len(lst) == 0:
+ return lst
+ if isinstance(lst[0], list):
+ return flatten(lst[0]) + flatten(lst[1:])
+ return lst[:1] + flatten(lst[1:])