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.collapse

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-a-list-of-lists>).
>  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-le...@python.org
> https://mail.python.org/mailman3/lists/numpy-discussion.python.org/
> Member address: dom.grigo...@gmail.com

_______________________________________________
NumPy-Discussion mailing list -- numpy-discussion@python.org
To unsubscribe send an email to numpy-discussion-le...@python.org
https://mail.python.org/mailman3/lists/numpy-discussion.python.org/
Member address: arch...@mail-archive.com

Reply via email to