Iteratively flatten a list shallowly or deeply.
(array, depth=-1)
| 1 | def iterflatten(array, depth=-1): |
| 2 | """Iteratively flatten a list shallowly or deeply.""" |
| 3 | for item in array: |
| 4 | if isinstance(item, (list, tuple)) and depth != 0: |
| 5 | for subitem in iterflatten(item, depth - 1): |
| 6 | yield subitem |
| 7 | else: |
| 8 | yield item |
| 9 | |
| 10 | def flatten_depth(array, depth=1): |
| 11 | """ |