Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Flatten a List by johan4622
"""
[Flatten a Lis](https://py.checkio.org/en/mission/flatten-list/)
"""
from typing import List
def flat_list(array : List[any]) -> List[int]:
"""
return flatten list.
Parameters
----------
- array : List[any]
a nested list with integers.
Returns
-------
- result : List[integer]
The one-dimensional list with integers.
"""
return [z for y in array for z in (flat_list(y) if hasattr(y, '__iter__') else (y,))]
if __name__ == '__main__':
assert flat_list([1, 2, 3]) == [1, 2, 3], "First"
assert flat_list([1, [2, 2, 2], 4]) == [1, 2, 2, 2, 4], "Second"
assert flat_list([[[2]], [4, [5, 6, [6], 6, 6, 6], 7]]) == [2, 4, 5, 6, 6, 6, 6, 6, 7], "Third"
assert flat_list([-1, [1, [-2], 1], -1]) == [-1, 1, -2, 1, -1], "Four"
print('Done! Check it')
May 18, 2019
Comments: