• flattening lists

    From SquidBits _@21:1/5 to All on Tue Oct 11 12:32:23 2022
    Does anyone else think there should be a flatten () function, which just turns a multi-dimensional list into a one-dimensional list in the order it's in. e.g.

    [[1,2,3],[4,5,6,7],[8,9]] becomes [1,2,3,4,5,6,7,8,9].

    I have had to flatten lists quite a few times and it's quite tedious to type out. It feels like this should be something built in to python, anyone else think this way?

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Larry Martell@21:1/5 to giladsmum@gmail.com on Tue Oct 11 12:58:53 2022
    On Tue, Oct 11, 2022 at 12:48 PM SquidBits _ <giladsmum@gmail.com> wrote:

    Does anyone else think there should be a flatten () function, which just turns a multi-dimensional list into a one-dimensional list in the order it's in. e.g.

    [[1,2,3],[4,5,6,7],[8,9]] becomes [1,2,3,4,5,6,7,8,9].

    I have had to flatten lists quite a few times and it's quite tedious to type out. It feels like this should be something built in to python, anyone else think this way?

    x = [[1,2,3],[4,5,6,7],[8,9]]
    [i for j in x for i in j]
    [1, 2, 3, 4, 5, 6, 7, 8, 9]

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Stefan Ram@21:1/5 to giladsmum@gmail.com on Tue Oct 11 20:01:12 2022
    SquidBits _ <giladsmum@gmail.com> writes:
    I have had to flatten lists quite a few times and it's quite
    tedious to type out. It feels like this should be something
    built in to python, anyone else think this way?

    Isn't this a question of how you organize your code? Usually
    you would paste the definition into your personal library
    and use it from there the next time you need it after having
    searched for it in the documentation of your personal library.

    OT: When I was young, I was learning LISP and wrote a
    flatten function in LISP. It looked something like

    ( SETQ FLATTEN
    ( LAMBDA( X )
    ( COND
    ( ( NULLP X ) '() )
    ( ( ATOMP X ) X )
    ( T ( APPEND
    ( COND
    ( ( ATOMP( CAR X ))( LIST( CAR X )))
    ( T ( FLATTEN( CAR X ))))
    ( FLATTEN( CDR X )))))))

    . Here's a quick attempt at it in Python:

    source = [[1,2,3],[4,5,6,7],[8,9]]

    def flatten( source ):
    if isinstance( source, list ):
    for entry in source:
    yield from flatten( entry )
    else: yield source

    print( [ *flatten( source )])

    . I never understood "yield from" until just now, when I was
    thinking, "Maybe this could be the piece that fits in here!"

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Stefan Ram@21:1/5 to Stefan Ram on Tue Oct 11 20:09:45 2022
    ram@zedat.fu-berlin.de (Stefan Ram) writes:
    . I never understood "yield from" until just now, when I was
    thinking, "Maybe this could be the piece that fits in here!"

    PS: If I'm starting to think about it: Having succeeded
    after using it by trial in one case does not mean that I
    have understood it!

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From David Lowry-Duda@21:1/5 to All on Tue Oct 11 16:02:55 2022
    On Tue, Oct 11, 2022 at 12:32:23PM -0700, SquidBits _ wrote:
    Does anyone else think there should be a flatten () function, which just turns a multi-dimensional list into a one-dimensional list in the order it's in. e.g.

    [[1,2,3],[4,5,6,7],[8,9]] becomes [1,2,3,4,5,6,7,8,9].

    I have had to flatten lists quite a few times and it's quite tedious to
    type out. It feels like this should be something built in to python,
    anyone else think this way?

    I typically don't mind things that are one liners, especially if the one
    liner is a list comprehension.


    def flatten1(inlist):
    return [l for sublist in inlist for l in sublist]

    givenlist = [[1, 2, 3], [4, 5, 6, 7], [8, 9]]
    print(flatten1(givenlist))

    def flatten2(inlist):
    return sum(inlist, [])

    print(flatten2(givenlist))


    Looking up "flatten" in python's source reveals the (not at all obvious
    to me as I don't use chain) alternative


    import itertools
    def flatten3(inlist):
    return list(itertools.chain.from_iterable)

    print(flatten3(givenlist))


    I notice that "flatten" appears many times in python's source. I didn't
    check how many times it's used with the same meaning, though.

    - DLD

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From dn@21:1/5 to All on Wed Oct 12 10:08:07 2022
    On 12/10/2022 08.32, SquidBits _ wrote:
    Does anyone else think there should be a flatten () function, which just turns a multi-dimensional list into a one-dimensional list in the order it's in. e.g.

    [[1,2,3],[4,5,6,7],[8,9]] becomes [1,2,3,4,5,6,7,8,9].

    I have had to flatten lists quite a few times and it's quite tedious to type out. It feels like this should be something built in to python, anyone else think this way?


    There is a flatten function!

    First though, are we ONLY talking about 'flattening' a 2D list (per
    example, above) or might the requirements extend to multiple-dimensions?
    The solutions vary accordingly!


    Two-dimensions:
    (don't think this method previously-mentioned, but very readable)

    l = [[1,2,3],[4,5,6,7],[8,9]]
    flattened = list()
    for element in l:
    ... flattened += element
    ...
    flattened
    [1, 2, 3, 4, 5, 6, 7, 8, 9]

    (NB if "l" were three-dimensional, "flattened" would become 2D)


    Multi-dimensional:
    Reach for itertools: (https://docs.python.org/3/library/itertools.html#itertools.chain)

    import itertools as it
    iter_flattened = it.chain( *l )
    list( iter_flattened )
    [1, 2, 3, 4, 5, 6, 7, 8, 9]


    Wrt "I have had to flatten lists quite a few times and it's quite
    tedious to type out.", isn't this a "code-smell"?

    Certainly motivation to generalise and write a solution as a function.
    Do it once, and do it right!

    Hence advice elsewhere to build a list-processing utility-library.

    On the other hand, has it already been done for us?


    An exercise for the reader:
    is reaching for itertools 'over-kill' in 2D?
    - speed-comparison between loading the itertools library and then
    employing the speedy method, or using a (built-in) for-loop at
    Python-speed with no import-overhead?
    (results will vary significantly according to len( l ), but do they
    remain consistently in-favor or one method or the other?)
    --
    Regards,
    =dn

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Thomas Passin@21:1/5 to All on Tue Oct 11 16:27:07 2022
    Is this what you usually do?

    l1 = [[1,2,3],[4,5,6,7],[8,9]]
    l2 = []
    for lz in l1:
    l2.extend(lz)

    print(l2) # [1,2,3,4,5,6,7,8,9]

    Not all that "tedious", perhaps... I tend to accumulate little utilities
    like this in a file and point to it with a .pth file. Of course, you
    have to remember to include it if you share your program with someone else.

    On 10/11/2022 3:32 PM, SquidBits _ wrote:
    Does anyone else think there should be a flatten () function, which just turns a multi-dimensional list into a one-dimensional list in the order it's in. e.g.

    [[1,2,3],[4,5,6,7],[8,9]] becomes [1,2,3,4,5,6,7,8,9].

    I have had to flatten lists quite a few times and it's quite tedious to type out. It feels like this should be something built in to python, anyone else think this way?

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From MRAB@21:1/5 to Stefan Ram on Wed Oct 12 00:26:10 2022
    On 2022-10-11 21:09, Stefan Ram wrote:
    ram@zedat.fu-berlin.de (Stefan Ram) writes:
    . I never understood "yield from" until just now, when I was
    thinking, "Maybe this could be the piece that fits in here!"

    PS: If I'm starting to think about it: Having succeeded
    after using it by trial in one case does not mean that I
    have understood it!


    This:

    yield from iterable

    is equivalent to:

    for item in iterable:
    yield item

    but is more efficient.

    That's really all you need to know!

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Dan Stromberg@21:1/5 to giladsmum@gmail.com on Tue Oct 11 19:46:21 2022
    On Tue, Oct 11, 2022 at 12:48 PM SquidBits _ <giladsmum@gmail.com> wrote:

    Does anyone else think there should be a flatten () function, which just turns a multi-dimensional list into a one-dimensional list in the order
    it's in. e.g.

    [[1,2,3],[4,5,6,7],[8,9]] becomes [1,2,3,4,5,6,7,8,9].

    I have had to flatten lists quite a few times and it's quite tedious to
    type out. It feels like this should be something built in to python, anyone else think this way?


    I think the usual argument against putting something like this in the
    standard library (I hope it won't be built in), is that there are many ways
    to define "flatten". That is, should it only go one level deep? All the
    way to the bottom? n levels deep? Should it do something special with
    lists, dicts, tuples, sets?

    This looks like a nice URL on the topic: https://www.pythonpool.com/flatten-list-python/

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Antoon Pardon@21:1/5 to All on Wed Oct 12 18:45:39 2022
    Op 11/10/2022 om 21:32 schreef SquidBits _:
    Does anyone else think there should be a flatten () function, which just turns a multi-dimensional list into a one-dimensional list in the order it's in. e.g.

    [[1,2,3],[4,5,6,7],[8,9]] becomes [1,2,3,4,5,6,7,8,9].

    I have had to flatten lists quite a few times and it's quite tedious to type out. It feels like this should be something built in to python, anyone else think this way?

    Depending on what you exactly mean by "flatten", it already is easy to
    flatten a list in python:

    lst = [[1,2,3],[4,5,6,7],[8,9]]
    list(itertools.chain.from_iterable(lst))
    [1, 2, 3, 4, 5, 6, 7, 8, 9]

    --
    Antoon Pardon

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)