This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: itertools -> flatten_all()
Type: enhancement Stage:
Components: Documentation Versions: Python 3.6
process
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: rhettinger Nosy List: YoSTEALTH, rhettinger, serhiy.storchaka
Priority: low Keywords:

Created on 2016-08-24 17:29 by YoSTEALTH, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (4)
msg273581 - (view) Author: (YoSTEALTH) * Date: 2016-08-24 17:29
# Maybe a Recipe for itertools

from collections.abc import Iterable

def flatten_all(iterable):
    # -> 'one'
    # <- ['one']
    # -> ['one', [b'two', b'three'], ['four', ('five', (1, {'e', 'ee'}, (2, (3, ))), ['six'])], generator()]
    # <- ['one', b'two', b'three', 'four', 'five', 1, 'ee', 'e', 2, 3, 'six', 0, 1, 2]

    if isinstance(iterable, Iterable) and not isinstance(iterable, (str, bytes)):
        for it in iterable:
            yield from flatten_all(it)
    else:  # int & others types as is.
        yield iterable


if __name__ == "__main__":

    # Test Only
    def generator():
        for i in range(3):
            yield i

    a = ['one', [b'two', b'three'], ['four', ('five', (1, {'e', 'ee'}, (2, (3, ))), ['six'])], generator()]
    # a = 'one'
    # a = (True, False)

    print(list(flatten_all(a)))
msg273669 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2016-08-25 16:46
In proposed implementation str and bytes are not flattened. What about bytearray? array.array? memoryview? For different tasks they can be considered atomic or as collections. There are such much task specific details and the implementation is such simple, that I think there is no need add this function in itertools. And due to this details it may be not worth to add it as a recipe.
msg273683 - (view) Author: (YoSTEALTH) * Date: 2016-08-25 21:59
Currently there is flatten() function in itertools Recipes section. This is what it does:
-> a = ['one', 'plus', [b'two', b'three'], ['four', ('five', (1, {'e', 'ee'}, (2, (3, ))), ['six'])], generator()]
<- ['o', 'n', 'e', 'p', 'l', 'u', 's', b'two', b'three', 'four', ('five', (1, {'ee', 'e'}, (2, (3,))), ['six']), 0, 1, 2]

As you can see it only flattens 1 nested level and for some reason it explodes the 'one' and 'plus' str items (maybe intentionally designed or lazily ignored, i don't know).

This is useful maybe in special circumstances where you have:
-> a = [[1, 2, 3], ['one', 'two', 'three']]
<- [1, 2, 3, 'one', 'two', 'three']

Also it doesn't work with bytearray, memoryview ...


In real worldly use something like flatten_all() is more useful!
msg273689 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2016-08-26 03:22
> I think there is no need add this function in itertools. 
> And due to this details it may be not worth to add it as a recipe.

Sorry YoSTEALTH, I concur with Serhiy on both points and am going to pass on this proposal. 

That said, I encourage you to post elsewhere.  When my own ideas don't make it to the docs or standard library, I usually post a recipe in the ASPN Python Cookbook (for example, see https://code.activestate.com/recipes/users/178123/ ).
History
Date User Action Args
2022-04-11 14:58:35adminsetgithub: 72039
2016-08-26 03:22:57rhettingersetstatus: open -> closed
resolution: rejected
messages: + msg273689
2016-08-25 21:59:36YoSTEALTHsetmessages: + msg273683
2016-08-25 16:46:38serhiy.storchakasetnosy: + serhiy.storchaka
messages: + msg273669
2016-08-25 07:42:05rhettingersetpriority: normal -> low
assignee: rhettinger

components: + Documentation
nosy: + rhettinger
2016-08-24 17:29:50YoSTEALTHcreate