diff options
author | Bryan Bishop <kanzure@gmail.com> | 2013-09-01 15:57:02 -0500 |
---|---|---|
committer | Bryan Bishop <kanzure@gmail.com> | 2013-09-01 15:57:02 -0500 |
commit | f666fcb67de7ed9893873dc87462716bef0155b3 (patch) | |
tree | 387f1bc06dce95ccd703adfcbebd8bde1f03da78 /pokemontools/helpers.py | |
parent | a1d9fd778dd8c86c810583c9c6bf76f709a35041 (diff) |
move flatten and flattener into helpers
Diffstat (limited to 'pokemontools/helpers.py')
-rw-r--r-- | pokemontools/helpers.py | 15 |
1 files changed, 15 insertions, 0 deletions
diff --git a/pokemontools/helpers.py b/pokemontools/helpers.py index e2f62e0..42cec87 100644 --- a/pokemontools/helpers.py +++ b/pokemontools/helpers.py @@ -12,3 +12,18 @@ def grouper(some_list, count=2): given: [1, 2, 3, 4] returns: [[1, 2], [3, 4]]""" return [some_list[i:i+count] for i in range(0, len(some_list), count)] + +def flattener(x): + "flattens a list of sublists into just one list (generator)" + try: + it = iter(x) + except TypeError: + yield x + else: + for i in it: + for j in flattener(i): + yield j + +def flatten(x): + "flattens a list of sublists into just one list" + return list(flattener(x)) |