Proposing a flattening functionality for deeply nested lists in NumPy
![](https://secure.gravatar.com/avatar/fe5b79e2b249ec21e456d5ea096eb74b.jpg?s=120&d=mm&r=g)
Hello all, Many people have asked how to flatten a nested list into a one-dimensional list (e.g., see this StackOverflow thread <https://stackoverflow.com/questions/952914/how-do-i-make-a-flat-list-out-of-...> ). While flattening a 2D list is relatively straightforward, deeply nested lists can become cumbersome to handle. To address this challenge, I propose adding a built-in list-flattening functionality to NumPy. By adding this feature to NumPy, the library would not only simplify a frequently used task but also enhance its overall usability, making it an even more powerful tool for data manipulation and scientific computing. The code snippet below demonstrates how a nested list can be flattened, enabling conversion into a NumPy array. I believe this would be a valuable addition to the package. See also this issue <https://github.com/numpy/numpy/issues/28079>. from collections.abc import Iterable def flatten_list(iterable): """ Flatten a (nested) list into a one-dimensional list. Parameters ---------- iterable : iterable The input collection. Returns ------- flattened_list : list A one-dimensional list containing all the elements from the input, with any nested structures flattened. Examples -------- Flattening a list containing nested lists: >>> obj = [[1, 2, 3], [1, 2, 3]] >>> flatten_list(obj) [1, 2, 3, 1, 2, 3] Flattening a list with sublists of different lengths: >>> obj = [1, [7, 4], [8, 1, 5]] >>> flatten_list(obj) [1, 7, 4, 8, 1, 5] Flattening a deeply nested list. >>> obj = [1, [2], [[3]], [[[4]]],] >>> flatten_list(obj) [1, 2, 3, 4] Flattening a list with various types of elements: >>> obj = [1, [2], (3), (4,), {5}, np.array([1,2,3]), range(3), 'Hello'] >>> flatten_list(obj) [1, 2, 3, 4, 5, 1, 2, 3, 0, 1, 2, 'Hello'] """ if not isinstance(iterable, Iterable) or isinstance(iterable, str): return [iterable] def flatten_generator(iterable): for item in iterable: if isinstance(item, Iterable) and not isinstance(item, str): yield from flatten_generator(item) else: yield item return list(flatten_generator(iterable))
![](https://secure.gravatar.com/avatar/72f994ca072df3a3d2c3db8a137790fd.jpg?s=120&d=mm&r=g)
On Tue, 31 Dec 2024 at 06:41, Mark via NumPy-Discussion < numpy-discussion@python.org> wrote:
Hello all,
Many people have asked how to flatten a nested
I think this is out of scope for NumPy. Our bar for adding functionality is quite high. We are unlikely to consider generic routines that are not directly connected to NumPy style homogenous ND arrays, and even then will look first for consensus with the Data Array API standard. In truth, our direction is to deprecate and remove parts of the library that are not directly related to the Array API. Matti
![](https://secure.gravatar.com/avatar/8dc4d0010b050a4bae0b632defb5c4f3.jpg?s=120&d=mm&r=g)
Hi Mark, I think this has already been implemented. For Iterables (list included), there is `more_itertools.collapse`, which you can use to achieve examples in your code as `list(more_itertools.collapse(list))`. See: https://more-itertools.readthedocs.io/en/stable/api.html#more_itertools.coll... If you don’t mind intermediate conversions: a) For well behaved arrays, such as `numpy.ndarray`, you can do `np.ravel(list).tolist()` or there is a bit more functional `torch.flatten` (I think `numpy` could add this functionality too - it is quite useful). b) For ragged arrays, there is `awkward.flatten`. Given that your need is not related to `numpy.ndarray` objects, I don’t think `numpy` is the right place for it. I would alternatively suggest exploring `more_itertools` and if something is missing, issue `Iterator` related proposals over there.
On 31 Dec 2024, at 06:37, Mark via NumPy-Discussion <numpy-discussion@python.org> wrote:
Hello all,
Many people have asked how to flatten a nested list into a one-dimensional list (e.g., see this StackOverflow thread <https://stackoverflow.com/questions/952914/how-do-i-make-a-flat-list-out-of-...>). While flattening a 2D list is relatively straightforward, deeply nested lists can become cumbersome to handle. To address this challenge, I propose adding a built-in list-flattening functionality to NumPy.
By adding this feature to NumPy, the library would not only simplify a frequently used task but also enhance its overall usability, making it an even more powerful tool for data manipulation and scientific computing.
The code snippet below demonstrates how a nested list can be flattened, enabling conversion into a NumPy array. I believe this would be a valuable addition to the package. See also this issue <https://github.com/numpy/numpy/issues/28079>.
from collections.abc import Iterable
def flatten_list(iterable):
""" Flatten a (nested) list into a one-dimensional list.
Parameters ---------- iterable : iterable The input collection.
Returns ------- flattened_list : list A one-dimensional list containing all the elements from the input, with any nested structures flattened.
Examples --------
Flattening a list containing nested lists:
>>> obj = [[1, 2, 3], [1, 2, 3]] >>> flatten_list(obj) [1, 2, 3, 1, 2, 3]
Flattening a list with sublists of different lengths:
>>> obj = [1, [7, 4], [8, 1, 5]] >>> flatten_list(obj) [1, 7, 4, 8, 1, 5]
Flattening a deeply nested list.
>>> obj = [1, [2], [[3]], [[[4]]],] >>> flatten_list(obj) [1, 2, 3, 4]
Flattening a list with various types of elements:
>>> obj = [1, [2], (3), (4,), {5}, np.array([1,2,3]), range(3), 'Hello'] >>> flatten_list(obj) [1, 2, 3, 4, 5, 1, 2, 3, 0, 1, 2, 'Hello']
"""
if not isinstance(iterable, Iterable) or isinstance(iterable, str): return [iterable]
def flatten_generator(iterable):
for item in iterable:
if isinstance(item, Iterable) and not isinstance(item, str): yield from flatten_generator(item)
else: yield item
return list(flatten_generator(iterable))
_______________________________________________ NumPy-Discussion mailing list -- numpy-discussion@python.org To unsubscribe send an email to numpy-discussion-leave@python.org https://mail.python.org/mailman3/lists/numpy-discussion.python.org/ Member address: dom.grigonis@gmail.com
participants (3)
-
Dom Grigonis
-
Mark
-
matti picus