Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

6 May, 2024: Currently experiencing networking issues at backend news servers. No resolution at this time.


devel / comp.lang.python / flattening lists

SubjectAuthor
* flattening listsSquidBits _
+- Re: flattening listsLarry Martell
+* Re: flattening listsStefan Ram
|`* Re: flattening listsStefan Ram
| `- Re: flattening listsMRAB
+- Re: flattening listsDavid Lowry-Duda
+- Re: flattening listsThomas Passin
+- Re: flattening listsdn
+- Re: flattening listsDan Stromberg
`- Re: flattening listsAntoon Pardon

1
flattening lists

<10d193c8-0dbe-41c8-9c63-17e4c04f1c97n@googlegroups.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=19801&group=comp.lang.python#19801

  copy link   Newsgroups: comp.lang.python
X-Received: by 2002:a05:620a:b51:b0:6cf:68b2:d86e with SMTP id x17-20020a05620a0b5100b006cf68b2d86emr17991891qkg.176.1665516744493;
Tue, 11 Oct 2022 12:32:24 -0700 (PDT)
X-Received: by 2002:a05:6808:1186:b0:353:b77a:a481 with SMTP id
j6-20020a056808118600b00353b77aa481mr362921oil.199.1665516744162; Tue, 11 Oct
2022 12:32:24 -0700 (PDT)
Path: i2pn2.org!i2pn.org!usenet.blueworldhosting.com!feed1.usenet.blueworldhosting.com!peer03.iad!feed-me.highwinds-media.com!news.highwinds-media.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.python
Date: Tue, 11 Oct 2022 12:32:23 -0700 (PDT)
Injection-Info: google-groups.googlegroups.com; posting-host=86.9.171.78; posting-account=q8bxYwoAAABRA8Z5dOA1qQu_xJJNhLE4
NNTP-Posting-Host: 86.9.171.78
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <10d193c8-0dbe-41c8-9c63-17e4c04f1c97n@googlegroups.com>
Subject: flattening lists
From: gilads...@gmail.com (SquidBits _)
Injection-Date: Tue, 11 Oct 2022 19:32:24 +0000
Content-Type: text/plain; charset="UTF-8"
X-Received-Bytes: 1355
 by: SquidBits _ - Tue, 11 Oct 2022 19:32 UTC

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?

Re: flattening lists

<mailman.653.1665518373.20444.python-list@python.org>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=19805&group=comp.lang.python#19805

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!aioe.org!news.mb-net.net!open-news-network.org!news.mind.de!bolzen.all.de!fu-berlin.de!uni-berlin.de!not-for-mail
From: larry.ma...@gmail.com (Larry Martell)
Newsgroups: comp.lang.python
Subject: Re: flattening lists
Date: Tue, 11 Oct 2022 12:58:53 -0700
Lines: 11
Message-ID: <mailman.653.1665518373.20444.python-list@python.org>
References: <10d193c8-0dbe-41c8-9c63-17e4c04f1c97n@googlegroups.com>
<CACwCsY7OLzWv8njot_4Z9RTXvBrTyAXKBYrQ3=N4Vf2G5QvJng@mail.gmail.com>
Mime-Version: 1.0
Content-Type: text/plain; charset="UTF-8"
X-Trace: news.uni-berlin.de gwLIoKqf4TcGzzJYyk4qpAzdvE2sEfUhLrxh8kl/kzlA==
Return-Path: <larry.martell@gmail.com>
X-Original-To: python-list@python.org
Delivered-To: python-list@mail.python.org
Authentication-Results: mail.python.org; dkim=pass
reason="2048-bit key; unprotected key"
header.d=gmail.com header.i=@gmail.com header.b=INdzRb8w;
dkim-adsp=pass; dkim-atps=neutral
X-Spam-Status: OK 0.022
X-Spam-Evidence: '*H*': 0.96; '*S*': 0.00; '2022': 0.05; 'e.g.': 0.07;
'skip:[ 20': 0.09; 'way?': 0.09; 'received:209.85.215.171': 0.16;
'received:mail-pg1-f171.google.com': 0.16; 'subject:lists': 0.16;
'tedious': 0.16; 'turns': 0.16; 'wrote:': 0.16; 'tue,': 0.19;
'to:addr:python-list': 0.20; 'python,': 0.25; 'anyone': 0.25;
'11,': 0.26; 'else': 0.27; 'think': 0.32; 'message-
id:@mail.gmail.com': 0.32; 'there': 0.33; 'header:In-Reply-To:1':
0.34; 'received:google.com': 0.34; 'from:addr:gmail.com': 0.35;
'built': 0.36; 'lists': 0.37; "it's": 0.37; 'received:209.85':
0.37; 'received:209': 0.39; 'quite': 0.39; 'list': 0.39;
'something': 0.40; 'should': 0.40; 'skip:o 10': 0.61; 'becomes':
0.64; 'in.': 0.64; 'order': 0.69; 'times': 0.69; 'out.': 0.80
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112;
h=to:subject:message-id:date:from:in-reply-to:references:mime-version
:from:to:cc:subject:date:message-id:reply-to;
bh=TTb7VQWhGEV1YliWi6bmLecSAXOTsC4Uk1QX5Q57sHA=;
b=INdzRb8wLKWqwloHbifaxGw+l3FFuC4YN5XOxP9Dy3kONesxzqYFD41a9iDnxX7u4D
PnG7MkGEc6on/27qQDkdbpNur1USxqR1zfX2Pn+gZ03BnNy+gIYwNNcdvdAVsuHxuMS8
EOT9VvQPYUZpj+nFDuvEhSBk3xv7KQ90fVAfp5FfUd6r4MvnTzeLs3PukcD2Fi2q9ofO
UKexVVXHeImaiN+KeBlE6SpPpFEautKxAn33dGW4NOW69I3z0bTo1kBbmBrniTWhcW6F
PegW+nd6wYXeTUIvtqwAadAD8IUkCKOf6RolqbmihcWyJy1asPfYcntGDzQAsytJ19VG
5x+A==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=1e100.net; s=20210112;
h=to:subject:message-id:date:from:in-reply-to:references:mime-version
:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to;
bh=TTb7VQWhGEV1YliWi6bmLecSAXOTsC4Uk1QX5Q57sHA=;
b=59gtOlItv4p6UgvAhEru9oE+/wZ6Kwm3S2z39VhfY/1/wj9hENecvN2hA9IC5lVhHf
2sfiwHsn2OdFCYEGA+EdmTwV30hS8UAHOThCvu5yD2C1Im+j/AddrDFaBtExpC5KTiEr
Rm+YHVGFlANNqNurNXq4VzspKphtOhG/eSVH7B1SD64PWPoHAgd20vhYaQlNpBACfvkx
4gUWeRR8/MoCYlJr7WjvsBE3SOSmUTj0lJM+X3C9tgmdH2Kv9jhdg2WmTB/9/bdxYR6o
hG7Zu7179Q7XXhtBAXw8eQIGnY7zK+L8eyLkIHQ1Yfi8j0vibZqqZ5iVKXJIRAIkjdlM
wFwA==
X-Gm-Message-State: ACrzQf3yxNlP/DiyPXhBmxeHytfaka+dytb8VusSTq05htX343265FYr
YaDKuPZTSvMqkfMypmx8HyGsR6YPxlI4VVTItwZIWVIogA==
X-Google-Smtp-Source: AMsMyM51t20fg4lor0savtLXaNW+v92CrLE/qCTtUltFA+H3NdWogQs8Zl3q0xMs/55ASMDoLi0NcQBTL/gxVG3gl4g=
X-Received: by 2002:a63:2ac9:0:b0:457:24a2:6fa7 with SMTP id
q192-20020a632ac9000000b0045724a26fa7mr21980810pgq.484.1665518369779; Tue, 11
Oct 2022 12:59:29 -0700 (PDT)
In-Reply-To: <10d193c8-0dbe-41c8-9c63-17e4c04f1c97n@googlegroups.com>
X-BeenThere: python-list@python.org
X-Mailman-Version: 2.1.39
Precedence: list
List-Id: General discussion list for the Python programming language
<python-list.python.org>
List-Unsubscribe: <https://mail.python.org/mailman/options/python-list>,
<mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive: <https://mail.python.org/pipermail/python-list/>
List-Post: <mailto:python-list@python.org>
List-Help: <mailto:python-list-request@python.org?subject=help>
List-Subscribe: <https://mail.python.org/mailman/listinfo/python-list>,
<mailto:python-list-request@python.org?subject=subscribe>
X-Mailman-Original-Message-ID: <CACwCsY7OLzWv8njot_4Z9RTXvBrTyAXKBYrQ3=N4Vf2G5QvJng@mail.gmail.com>
X-Mailman-Original-References: <10d193c8-0dbe-41c8-9c63-17e4c04f1c97n@googlegroups.com>
 by: Larry Martell - Tue, 11 Oct 2022 19:58 UTC

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]

Re: flattening lists

<flatten-20221011205903@ram.dialup.fu-berlin.de>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=19807&group=comp.lang.python#19807

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!aioe.org!news.mb-net.net!open-news-network.org!news.mind.de!bolzen.all.de!fu-berlin.de!uni-berlin.de!not-for-mail
From: ram...@zedat.fu-berlin.de (Stefan Ram)
Newsgroups: comp.lang.python
Subject: Re: flattening lists
Date: 11 Oct 2022 20:01:12 GMT
Organization: Stefan Ram
Lines: 40
Expires: 1 Sep 2023 11:59:58 GMT
Message-ID: <flatten-20221011205903@ram.dialup.fu-berlin.de>
References: <10d193c8-0dbe-41c8-9c63-17e4c04f1c97n@googlegroups.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
X-Trace: news.uni-berlin.de ljthgda7eSFP0rZVoaKqMwFC1vsOh/2aMh+EgssQYEDMLK
X-Copyright: (C) Copyright 2022 Stefan Ram. All rights reserved.
Distribution through any means other than regular usenet
channels is forbidden. It is forbidden to publish this
article in the Web, to change URIs of this article into links,
and to transfer the body without this notice, but quotations
of parts in other Usenet posts are allowed.
X-No-Archive: Yes
Archive: no
X-No-Archive-Readme: "X-No-Archive" is set, because this prevents some
services to mirror the article in the web. But the article may
be kept on a Usenet archive server with only NNTP access.
X-No-Html: yes
Content-Language: en-US
Accept-Language: de-DE, en-US, it, fr-FR
 by: Stefan Ram - Tue, 11 Oct 2022 20:01 UTC

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!"

Re: flattening lists

<from-20221011210859@ram.dialup.fu-berlin.de>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=19808&group=comp.lang.python#19808

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!aioe.org!news.mb-net.net!open-news-network.org!news.mind.de!bolzen.all.de!fu-berlin.de!uni-berlin.de!not-for-mail
From: ram...@zedat.fu-berlin.de (Stefan Ram)
Newsgroups: comp.lang.python
Subject: Re: flattening lists
Date: 11 Oct 2022 20:09:45 GMT
Organization: Stefan Ram
Lines: 9
Expires: 1 Sep 2023 11:59:58 GMT
Message-ID: <from-20221011210859@ram.dialup.fu-berlin.de>
References: <10d193c8-0dbe-41c8-9c63-17e4c04f1c97n@googlegroups.com> <flatten-20221011205903@ram.dialup.fu-berlin.de>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
X-Trace: news.uni-berlin.de ptWwmeOQV6XYvXB/0vkVDAOAV5cj0gIs6LpT0nh7nmLGgi
X-Copyright: (C) Copyright 2022 Stefan Ram. All rights reserved.
Distribution through any means other than regular usenet
channels is forbidden. It is forbidden to publish this
article in the Web, to change URIs of this article into links,
and to transfer the body without this notice, but quotations
of parts in other Usenet posts are allowed.
X-No-Archive: Yes
Archive: no
X-No-Archive-Readme: "X-No-Archive" is set, because this prevents some
services to mirror the article in the web. But the article may
be kept on a Usenet archive server with only NNTP access.
X-No-Html: yes
Content-Language: en-US
Accept-Language: de-DE, en-US, it, fr-FR
 by: Stefan Ram - Tue, 11 Oct 2022 20:09 UTC

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!

Re: flattening lists

<mailman.655.1665519121.20444.python-list@python.org>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=19809&group=comp.lang.python#19809

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!news.uzoreto.com!fu-berlin.de!uni-berlin.de!not-for-mail
From: dav...@lowryduda.com (David Lowry-Duda)
Newsgroups: comp.lang.python
Subject: Re: flattening lists
Date: Tue, 11 Oct 2022 16:02:55 -0400
Lines: 40
Message-ID: <mailman.655.1665519121.20444.python-list@python.org>
References: <10d193c8-0dbe-41c8-9c63-17e4c04f1c97n@googlegroups.com>
<Y0XL78ucNN9POo3p@icerm-dld>
Mime-Version: 1.0
Content-Type: text/plain; charset=us-ascii; format=flowed
X-Trace: news.uni-berlin.de K9Ok1aAd0H1fWZFAZXazggUMtx0ihEY+DiXmRNB5+iRw==
Return-Path: <david@lowryduda.com>
X-Original-To: python-list@python.org
Delivered-To: python-list@mail.python.org
Authentication-Results: mail.python.org; dkim=pass
reason="1024-bit key; unprotected key"
header.d=lowryduda.com header.i=@lowryduda.com header.b=oDgULP5g;
dkim-adsp=pass; dkim-atps=neutral
X-Spam-Status: OK 0.006
X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'def': 0.04; '2022': 0.05;
"python's": 0.05; 'e.g.': 0.07; '-0700,': 0.09; 'skip:[ 20': 0.09;
'though.': 0.09; 'typically': 0.09; 'way?': 0.09; 'import': 0.15;
'itertools': 0.16; 'skip:> 20': 0.16; 'subject:lists': 0.16;
'tedious': 0.16; 'turns': 0.16; 'wrote:': 0.16; 'tue,': 0.19;
'to:addr:python-list': 0.20; 'python,': 0.25; 'anyone': 0.25;
'11,': 0.26; 'else': 0.27; 'think': 0.32; 'there': 0.33; 'same':
0.34; "didn't": 0.34; 'header:In-Reply-To:1': 0.34; 'built': 0.36;
'source': 0.36; 'lists': 0.37; "it's": 0.37; 'quite': 0.39;
'list': 0.39; 'use': 0.39; 'appears': 0.40; 'received:98': 0.40;
'something': 0.40; 'should': 0.40; 'skip:o 10': 0.61; 'becomes':
0.64; 'in.': 0.64; 'source.': 0.64; 'order': 0.69; 'obvious':
0.69; 'times': 0.69; 'out.': 0.80; 'received:23.106': 0.84;
'reveals': 0.91
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=lowryduda.com;
s=dkim; t=1665518584;
h=from:from:reply-to:subject:subject:date:date:message-id:message-id:
to:to:cc:mime-version:mime-version:content-type:content-type:
in-reply-to:in-reply-to:references:references;
bh=RUAVVQIFa4D3Dx8QTH1YtfdpOqIi37T2LXmfrLz2kiU=;
b=oDgULP5g8cV4jWUhCaVy4hdwccs0CsUjonwG1wOyUEqrKgRVcqLDeXNnYZtKkjxsJ1InGN
IlDdVjKDOY85LvWvSrk+yFB4+myMdJZfVTwYY2H/UUj42Ic4pwwhpgkSJjKokzfyC3Achm
8VpwEb/ri/NirJa/UqojCP1oRFx26wk=
Mail-Followup-To: python-list@python.org
Content-Disposition: inline
In-Reply-To: <10d193c8-0dbe-41c8-9c63-17e4c04f1c97n@googlegroups.com>
X-Spam-Status: No, score=0.40
X-BeenThere: python-list@python.org
X-Mailman-Version: 2.1.39
Precedence: list
List-Id: General discussion list for the Python programming language
<python-list.python.org>
List-Unsubscribe: <https://mail.python.org/mailman/options/python-list>,
<mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive: <https://mail.python.org/pipermail/python-list/>
List-Post: <mailto:python-list@python.org>
List-Help: <mailto:python-list-request@python.org?subject=help>
List-Subscribe: <https://mail.python.org/mailman/listinfo/python-list>,
<mailto:python-list-request@python.org?subject=subscribe>
X-Mailman-Original-Message-ID: <Y0XL78ucNN9POo3p@icerm-dld>
X-Mailman-Original-References: <10d193c8-0dbe-41c8-9c63-17e4c04f1c97n@googlegroups.com>
 by: David Lowry-Duda - Tue, 11 Oct 2022 20:02 UTC

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

Re: flattening lists

<mailman.657.1665520038.20444.python-list@python.org>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=19811&group=comp.lang.python#19811

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!aioe.org!news.uzoreto.com!fu-berlin.de!uni-berlin.de!not-for-mail
From: lis...@tompassin.net (Thomas Passin)
Newsgroups: comp.lang.python
Subject: Re: flattening lists
Date: Tue, 11 Oct 2022 16:27:07 -0400
Lines: 20
Message-ID: <mailman.657.1665520038.20444.python-list@python.org>
References: <10d193c8-0dbe-41c8-9c63-17e4c04f1c97n@googlegroups.com>
<76ef596d-6d00-178f-828f-a27fb6b02477@tompassin.net>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
X-Trace: news.uni-berlin.de 8YcmA+rMov7ED54PRZWRfA8hiT/hgDxf8yr+msFbs96w==
Return-Path: <list1@tompassin.net>
X-Original-To: python-list@python.org
Delivered-To: python-list@mail.python.org
Authentication-Results: mail.python.org; dkim=pass
reason="2048-bit key; unprotected key"
header.d=tompassin.net header.i=@tompassin.net header.b=rzhOPK0E;
dkim-adsp=pass; dkim-atps=neutral
X-Spam-Status: OK 0.046
X-Spam-Evidence: '*H*': 0.91; '*S*': 0.00; 'e.g.': 0.07; 'else.':
0.07; 'skip:[ 20': 0.09; 'way?': 0.09; 'received:10.0.0': 0.16;
'received:64.90': 0.16; 'received:64.90.62': 0.16;
'received:64.90.62.162': 0.16; 'subject:lists': 0.16; 'tedious':
0.16; 'turns': 0.16; 'utilities': 0.16; 'wrote:': 0.16; 'pm,':
0.19; 'to:addr:python-list': 0.20; 'python,': 0.25; 'anyone':
0.25; 'do?': 0.26; 'else': 0.27; 'header:User-Agent:1': 0.30;
'program': 0.31; 'think': 0.32; 'received:10.0': 0.32;
'received:mailchannels.net': 0.32;
'received:relay.mailchannels.net': 0.32; 'there': 0.33; 'someone':
0.34; 'header:In-Reply-To:1': 0.34; 'built': 0.36; 'lists': 0.37;
"it's": 0.37; 'file': 0.38; 'quite': 0.39; 'list': 0.39;
'something': 0.40; 'should': 0.40; 'remember': 0.61; 'skip:o 10':
0.61; 'share': 0.63; 'becomes': 0.64; 'in.': 0.64; 'your': 0.64;
'header:Received:6': 0.67; 'received:64': 0.67; 'order': 0.69;
'times': 0.69; 'little': 0.73; 'out.': 0.80; 'tend': 0.91
X-Sender-Id: dreamhost|x-authsender|tpassin@tompassin.net
ARC-Seal: i=1; s=arc-2022; d=mailchannels.net; t=1665520028; a=rsa-sha256;
cv=none;
b=BDulUBs17QyEfna0jCcakPfBTvZFmEFgY+WZ+kQwHf7JxJkD6jhV6R4vFMvqvhxw+5TDR4
GNjxvd7t/68eckazZAHtLmP2kTw+bMuvEviZ66iKzBjWdkD7JCrs6UzgI7A2rFjoWHZz2R
YDMXNshVcgKe+alT3gmI/fhU+wf8I3hjvKUpDL0qAVGBdw+PIGkxcmRF1kRq4S8zU9Mqn7
PpbO66QtFRFbPpjB2o42llVi2CsdzuqIzASusenFejDW9/QPD2FN6u/qqjP2/mJ32D4eW5
abH60BGfb2tYLiagMRtx3IgZwga3lVgRXhhw8p8Wx/3o133gx9E7Ju0HHnDcvQ==
ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed;
d=mailchannels.net; s=arc-2022; t=1665520028;
h=from:from:reply-to:subject:subject:date:date:message-id:message-id:
to:to:cc:mime-version:mime-version:content-type:content-type:
content-transfer-encoding:content-transfer-encoding:
in-reply-to:in-reply-to:references:references:dkim-signature;
bh=fUY6+qqIM99TMQuSWP1xc+o75efX4lGIbB9pD6HO284=;
b=b7++mukqMhR07Cou6Od+1b6ZBl5lKRFhGTLYTbawZcemnXRLQbNn6aucR4uax6uavOgq6v
gv/a3pWiIK4iFy6P5pJAYde1ehxsv8UIENBPubE5rRfT504KFVvX6WOEpcH0BRJfD/ihYS
CNCz5SQf7q5FBhWA94+RUDdvqPYU6emhih4eEeVAHBPfz2ELrcE/slem8rrdJK2hFqYtep
Ee4JhZ1uRdpTM9zQBskLkbIKRyk5DaakZf+onTYIOWtaTkJ9qjGR24wi7PbqvpCntGcXbp
ndshMYWoJNth4jeF3UZ9KoEeaJjFaSQshqKw35tyeVWOb8VBd5WDqDuav9XrOA==
ARC-Authentication-Results: i=1; rspamd-5798657bcf-xmgzp;
auth=pass smtp.auth=dreamhost smtp.mailfrom=list1@tompassin.net
X-Sender-Id: dreamhost|x-authsender|tpassin@tompassin.net
X-MC-Relay: Neutral
X-MailChannels-SenderId: dreamhost|x-authsender|tpassin@tompassin.net
X-MailChannels-Auth-Id: dreamhost
X-Illustrious-Turn: 0a6bbf366277bdec_1665520029074_1602278840
X-MC-Loop-Signature: 1665520029074:2331298180
X-MC-Ingress-Time: 1665520029074
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=tompassin.net;
s=dreamhost; t=1665520028;
bh=fUY6+qqIM99TMQuSWP1xc+o75efX4lGIbB9pD6HO284=;
h=Date:Subject:To:From:Content-Type:Content-Transfer-Encoding;
b=rzhOPK0E0BSoJeUlgfRHAMzBo1gz/BbRaMY9TRK+aRPXubmcYHJhJgeL880CvDh7v
9AE6lYMW55jOJaRmqxUJfkuw2HDCJBNVM75Gii3pshqbK8rqgYaHloDz3kyN5qVA47
wlGKq4x9hY6IJWshApxFumeU1rAC0Eicmq//wpz6YXguKKEz2/ttgjWqus4sRsgp+T
tb3PPOUzZphIxswVLRuwYl6d9u6zay0AxpLcyjdCKR8+mu8F/rAvkA/FfdWwOxVyAn
LjQpRZl80rSIcC4+C/XtP+XX73N3LDxHc6NVbxnheZ455lrmcvwGYUwPIsJ6ZsDY5s
5CN/SPZ+kZHlA==
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101
Thunderbird/102.3.2
Content-Language: en-US
In-Reply-To: <10d193c8-0dbe-41c8-9c63-17e4c04f1c97n@googlegroups.com>
X-BeenThere: python-list@python.org
X-Mailman-Version: 2.1.39
Precedence: list
List-Id: General discussion list for the Python programming language
<python-list.python.org>
List-Unsubscribe: <https://mail.python.org/mailman/options/python-list>,
<mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive: <https://mail.python.org/pipermail/python-list/>
List-Post: <mailto:python-list@python.org>
List-Help: <mailto:python-list-request@python.org?subject=help>
List-Subscribe: <https://mail.python.org/mailman/listinfo/python-list>,
<mailto:python-list-request@python.org?subject=subscribe>
X-Mailman-Original-Message-ID: <76ef596d-6d00-178f-828f-a27fb6b02477@tompassin.net>
X-Mailman-Original-References: <10d193c8-0dbe-41c8-9c63-17e4c04f1c97n@googlegroups.com>
 by: Thomas Passin - Tue, 11 Oct 2022 20:27 UTC

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?

Re: flattening lists

<mailman.659.1665522501.20444.python-list@python.org>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=19813&group=comp.lang.python#19813

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From: PythonL...@DancesWithMice.info (dn)
Newsgroups: comp.lang.python
Subject: Re: flattening lists
Date: Wed, 12 Oct 2022 10:08:07 +1300
Organization: DWM
Lines: 60
Message-ID: <mailman.659.1665522501.20444.python-list@python.org>
References: <10d193c8-0dbe-41c8-9c63-17e4c04f1c97n@googlegroups.com>
<05f8fc2b-807b-ca62-cce4-6219b0e51690@DancesWithMice.info>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
X-Trace: news.uni-berlin.de 37NhCS6W4HbYyYBVyzWGXA3CMHycj/U1WhORR+YqkN0A==
Return-Path: <PythonList@DancesWithMice.info>
X-Original-To: python-list@python.org
Delivered-To: python-list@mail.python.org
Authentication-Results: mail.python.org; dkim=pass
reason="2048-bit key; unprotected key"
header.d=danceswithmice.info header.i=@danceswithmice.info
header.b=GflnM+Cp; dkim-adsp=pass; dkim-atps=neutral
X-Spam-Status: OK 0.012
X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'e.g.': 0.07; '=dn': 0.09;
'from:addr:danceswithmice.info': 0.09; 'from:addr:pythonlist':
0.09; 'received:192.168.1.64': 0.09; 'skip:[ 20': 0.09; 'way?':
0.09; 'import': 0.15; '(built-in)': 0.16; 'above)': 0.16;
'elsewhere': 0.16; 'employing': 0.16; 'hand,': 0.16; 'itertools':
0.16; 'list()': 0.16; 'message-id:@DancesWithMice.info': 0.16;
'received:51.254': 0.16; 'received:51.254.211': 0.16;
'received:51.254.211.219': 0.16; 'received:cloud': 0.16;
'received:rangi.cloud': 0.16; 'subject:lists': 0.16; 'tedious':
0.16; 'turns': 0.16; 'wrote:': 0.16; 'to:addr:python-list': 0.20;
'python,': 0.25; 'anyone': 0.25; 'library': 0.26; "isn't": 0.27;
'else': 0.27; 'done': 0.28; '>>>': 0.28; 'example,': 0.28; 'header
:User-Agent:1': 0.30; 'header:Organization:1': 0.31; 'think':
0.32; 'received:192.168.1': 0.32; 'but': 0.32; 'there': 0.33;
'header:In-Reply-To:1': 0.34; 'built': 0.36; 'lists': 0.37;
'using': 0.37; "it's": 0.37; 'received:192.168': 0.37; 'quite':
0.39; 'list': 0.39; 'something': 0.40; 'should': 0.40; 'method':
0.61; 'skip:o 10': 0.61; 'skip:m 20': 0.63; 'between': 0.63;
'becomes': 0.64; 'extend': 0.64; 'in.': 0.64; 'received:51': 0.64;
'order': 0.69; 'times': 0.69; 'reach': 0.69; 'solutions': 0.70;
'reaching': 0.71; '....': 0.76; 'us?': 0.76; 'vary': 0.76; 'out.':
0.80; 'exercise': 0.84; 'method,': 0.84
DKIM-Filter: OpenDKIM Filter v2.11.0 vps.rangi.cloud 1D49E922F
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=danceswithmice.info;
s=staff; t=1665522500;
bh=+f6cE6RF67+yrrwUNCd9NueMdvSHzhxUnALTyb36O8E=;
h=Date:Subject:To:References:From:In-Reply-To:From;
b=GflnM+Cpto5jO08umNyuNPQJjRhtvnJ/hBAmgLZ6hTsNCK6R1Xa4Mhrm/24QTDJH1
PG2yasydcSHpKPkLuj6ghQNGojE6jSSLK8J55KKv9lGBT8bXM++wEGoat4DOnOWJkd
TjfTZboqbCckHqtbCmBwfP2FvLJmw4nzosgdngnSDs+vSEvX9pOEoYQheR5oM+5Y1K
vSyje0mWzSUgjo8bvPk/PH5iLKpGT3VngKhDsuTpD1FnP84znWmKXQoPVSoqYmwi3L
cxXpX5WK+mxdb/v1Neqzk91l4BLYvNqQWNHZV1eoo3sbR/YdtWqNp//UBS7znCs+2H
tO/ph9drUFcSA==
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101
Thunderbird/102.3.2
Content-Language: en-GB
In-Reply-To: <10d193c8-0dbe-41c8-9c63-17e4c04f1c97n@googlegroups.com>
X-BeenThere: python-list@python.org
X-Mailman-Version: 2.1.39
Precedence: list
List-Id: General discussion list for the Python programming language
<python-list.python.org>
List-Unsubscribe: <https://mail.python.org/mailman/options/python-list>,
<mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive: <https://mail.python.org/pipermail/python-list/>
List-Post: <mailto:python-list@python.org>
List-Help: <mailto:python-list-request@python.org?subject=help>
List-Subscribe: <https://mail.python.org/mailman/listinfo/python-list>,
<mailto:python-list-request@python.org?subject=subscribe>
X-Mailman-Original-Message-ID: <05f8fc2b-807b-ca62-cce4-6219b0e51690@DancesWithMice.info>
X-Mailman-Original-References: <10d193c8-0dbe-41c8-9c63-17e4c04f1c97n@googlegroups.com>
 by: dn - Tue, 11 Oct 2022 21:08 UTC

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

Re: flattening lists

<mailman.664.1665530784.20444.python-list@python.org>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=19818&group=comp.lang.python#19818

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From: pyt...@mrabarnett.plus.com (MRAB)
Newsgroups: comp.lang.python
Subject: Re: flattening lists
Date: Wed, 12 Oct 2022 00:26:10 +0100
Lines: 23
Message-ID: <mailman.664.1665530784.20444.python-list@python.org>
References: <10d193c8-0dbe-41c8-9c63-17e4c04f1c97n@googlegroups.com>
<flatten-20221011205903@ram.dialup.fu-berlin.de>
<from-20221011210859@ram.dialup.fu-berlin.de>
<c1289872-6588-1b96-b418-c8e6f9b319f5@mrabarnett.plus.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
X-Trace: news.uni-berlin.de vVR16YL+VH02kHl3o4yuEwTbuCBIaDh+YOIVxhbMxPKA==
Return-Path: <python@mrabarnett.plus.com>
X-Original-To: python-list@python.org
Delivered-To: python-list@mail.python.org
Authentication-Results: mail.python.org; dkim=pass
reason="2048-bit key; unprotected key"
header.d=plus.com header.i=@plus.com header.b=ORUWInE6;
dkim-adsp=none (unprotected policy); dkim-atps=neutral
X-Spam-Status: OK 0.007
X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'this:': 0.03; 'ram': 0.07;
'from:addr:python': 0.09; 'received:192.168.1.64': 0.09;
'writes:': 0.09; 'from:addr:mrabarnett.plus.com': 0.16;
'from:name:mrab': 0.16; 'iterable': 0.16; 'message-
id:@mrabarnett.plus.com': 0.16; 'received:84.93': 0.16;
'received:84.93.230': 0.16; 'received:84.93.230.227': 0.16;
'received:plus.net': 0.16; 'subject:lists': 0.16; 'yield': 0.16;
'wrote:': 0.16; 'to:addr:python-list': 0.20; 'stefan': 0.26;
'header:User-Agent:1': 0.30; 'think': 0.32; 'received:192.168.1':
0.32; 'but': 0.32; "i'm": 0.33; 'mean': 0.34; 'header:In-Reply-
To:1': 0.34; 'item': 0.35; 'understood': 0.35; 'really': 0.37;
'using': 0.37; 'received:192.168': 0.37; 'could': 0.38; 'to:':
0.62; 'now,': 0.67; 'it:': 0.69; 'piece': 0.69; 'ps:': 0.69;
'trial': 0.84; 'succeeded': 0.84
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=plus.com; s=042019;
t=1665530776; bh=8NL0Xgc9+Ycuef3A9yznPgGeAbjvn0PIv6qYdPucvfM=;
h=Date:Subject:To:References:From:In-Reply-To;
b=ORUWInE6XFFRo3UD9fl+ol9wU3XSoZTP3ZJvb58p2N9vp5+ryomqXWmmTwkCQplr9
Po89p03DePysPVJso+dGTQhMGYOzYnc1jgtvqawsEuObPenGXM8fu+VypvliPC0buJ
OGFWxeDHNSDhByqJmydLZ8mA7B/XfwFEzbN8fcX2DokmsgteC+vWFh03H1/V6LlQn1
CFrk79v7FLcQSd58W1Oz15crgp9TSU2oA1e2U+aF1HB2n4TNDW6jz4A6+O8SWahi2r
qxzcFsXmAs2ylcoHDvNYCcFuU1Pwu/4s6/mZ1Ab/dewJDw+5sj/+AbsvLKk9OpdD4S
ubiWRA9UmxFDA==
X-Clacks-Overhead: "GNU Terry Pratchett"
X-CM-Score: 0.00
X-CNFS-Analysis: v=2.4 cv=eaIuwpIH c=1 sm=1 tr=0 ts=6345fb98
a=0nF1XD0wxitMEM03M9B4ZQ==:117 a=0nF1XD0wxitMEM03M9B4ZQ==:17
a=IkcTkHD0fZMA:10 a=DgfzaW0mLlOzppee-XYA:9 a=QEXdDO2ut3YA:10
X-AUTH: mrabarnett@:2500
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101
Thunderbird/102.3.2
Content-Language: en-GB
In-Reply-To: <from-20221011210859@ram.dialup.fu-berlin.de>
X-CMAE-Envelope: MS4xfK2lhUs1MPqxISOul98usJtR66GHAI51zW2zWLirCzyjCNf7v3qm/HLCozevv0DKmi76f8lgPtsbxUxXp2c7YHvzbECAMaBaKIoVBxAj/21GFwCBY2L9
C1y44aFJetranoXvXjxwrzuBRigY1+/1FOF9zxT86i9nHR3U8yGEqwm/8ivOUUfLwwPO00a4/X+/sF/JhFWblMyZwA7jQgKiEFg=
X-BeenThere: python-list@python.org
X-Mailman-Version: 2.1.39
Precedence: list
List-Id: General discussion list for the Python programming language
<python-list.python.org>
List-Unsubscribe: <https://mail.python.org/mailman/options/python-list>,
<mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive: <https://mail.python.org/pipermail/python-list/>
List-Post: <mailto:python-list@python.org>
List-Help: <mailto:python-list-request@python.org?subject=help>
List-Subscribe: <https://mail.python.org/mailman/listinfo/python-list>,
<mailto:python-list-request@python.org?subject=subscribe>
X-Mailman-Original-Message-ID: <c1289872-6588-1b96-b418-c8e6f9b319f5@mrabarnett.plus.com>
X-Mailman-Original-References: <10d193c8-0dbe-41c8-9c63-17e4c04f1c97n@googlegroups.com>
<flatten-20221011205903@ram.dialup.fu-berlin.de>
<from-20221011210859@ram.dialup.fu-berlin.de>
 by: MRAB - Tue, 11 Oct 2022 23:26 UTC

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!

Re: flattening lists

<mailman.667.1665542796.20444.python-list@python.org>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=19821&group=comp.lang.python#19821

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!weretis.net!feeder8.news.weretis.net!lilly.ping.de!fu-berlin.de!uni-berlin.de!not-for-mail
From: drsali...@gmail.com (Dan Stromberg)
Newsgroups: comp.lang.python
Subject: Re: flattening lists
Date: Tue, 11 Oct 2022 19:46:21 -0700
Lines: 21
Message-ID: <mailman.667.1665542796.20444.python-list@python.org>
References: <10d193c8-0dbe-41c8-9c63-17e4c04f1c97n@googlegroups.com>
<CAGGBd_ogqpaosQ0x=DnN34Gx-8Nb4iB9aktUD1oMPA2r7oAUyA@mail.gmail.com>
Mime-Version: 1.0
Content-Type: text/plain; charset="UTF-8"
X-Trace: news.uni-berlin.de xqzgWvFB4m7BtyACE518aQf4ni4WO0PTLEYmpGQYgWyA==
Return-Path: <drsalists@gmail.com>
X-Original-To: python-list@python.org
Delivered-To: python-list@mail.python.org
Authentication-Results: mail.python.org; dkim=pass
reason="2048-bit key; unprotected key"
header.d=gmail.com header.i=@gmail.com header.b=W2yLQUs1;
dkim-adsp=pass; dkim-atps=neutral
X-Spam-Status: OK 0.002
X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'looks': 0.02; 'argument':
0.04; '2022': 0.05; 'e.g.': 0.07; 'cc:addr:python-list': 0.09;
'skip:[ 20': 0.09; 'way?': 0.09; 'cc:no real name:2**0': 0.14;
'bottom?': 0.16; 'from:addr:drsalists': 0.16; 'from:name:dan
stromberg': 0.16; 'subject:lists': 0.16; 'tedious': 0.16;
'topic:': 0.16; 'tuples,': 0.16; 'turns': 0.16; 'url-
ip:104.21.13/24': 0.16; 'wrote:': 0.16; 'tue,': 0.19; 'url': 0.19;
'cc:addr:python.org': 0.20; 'python,': 0.25; 'cc:2**0': 0.25;
'anyone': 0.25; '11,': 0.26; 'library': 0.26; 'else': 0.27;
'putting': 0.31; 'think': 0.32; 'lists,': 0.32; 'message-
id:@mail.gmail.com': 0.32; 'there': 0.33; 'header:In-Reply-To:1':
0.34; 'received:google.com': 0.34; 'usual': 0.35;
'from:addr:gmail.com': 0.35; 'built': 0.36; 'lists': 0.37;
'special': 0.37; "it's": 0.37; 'received:209.85': 0.37; 'way':
0.38; 'received:209': 0.39; 'quite': 0.39; 'received:209.85.208':
0.39; 'list': 0.39; 'define': 0.40; 'something': 0.40; 'should':
0.40; 'url-ip:104.21/16': 0.61; 'skip:o 10': 0.61; '8bit%:9':
0.64; 'becomes': 0.64; 'in.': 0.64; 'order': 0.69; 'times': 0.69;
'out.': 0.80
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112;
h=cc:to:subject:message-id:date:from:in-reply-to:references
:mime-version:from:to:cc:subject:date:message-id:reply-to;
bh=PCxJq3CoQyWhewbXNCH2J/2F74RoDeZV/km3hd4BGdg=;
b=W2yLQUs1xJIRVvAtqKi+rELLtqVbrwGgUfCeqyAYeenGRxEVjVLq+rePte1f90py9/
HuG8jxnA5aCnw+8yElZaILlCFwhLEgjjMHFLeQbvi3mcBQx4l+bShpG0ZBHdz8Nue/wj
5fjktbmHR9aDxnBF10SyzolGlfIpHPHQMoB9ObHAMHqRxismHOnztPUVtg7h4ln46TuP
SPw/BChNf/Aia5ZtVvBcYTJ5d3aeij0uSUX2dZXxDn4nZ0mD57cmVo3ezsa2ewEJZYdR
UfXs4aMtzmGL/SrLZNSl0i86fwNjot58uElPFE0d0a30MTx9CuL7TgO5T0hVhd3qx9N9
Cytg==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=1e100.net; s=20210112;
h=cc:to:subject:message-id:date:from:in-reply-to:references
:mime-version:x-gm-message-state:from:to:cc:subject:date:message-id
:reply-to;
bh=PCxJq3CoQyWhewbXNCH2J/2F74RoDeZV/km3hd4BGdg=;
b=nsGabLOy6NAXau3WFVuoWgVbGfgC1eu/tqxdN9rztB3AOJ763eOJB182EPBViiD2Ze
VSeQpL/1oJwn/b8ufkbw3wiLQPlroSokWjgZtmoVRGZWdX3K2gOLjOwCvEK85BZcAeml
bQlNdUjpXQJlavlaNqioXsUf0PP5CmD2JELSY5Bpqd59qo3fuq6/SL16Ewqy/jM/G2fo
11YQAzysmf+0hJPbeCn4hrGBAdHiWDAGt4su6W/SmsU/arJ6XSGRp3+0TKrny/8pvL69
g4waZFGNSUDfUKYmD2HFHrywb+Uq3RrwSwnaLCE2k4EghwW0WeDr/y5SvdtOyvdakf8Y
6VBw==
X-Gm-Message-State: ACrzQf2ePj/vOZVoNI1qmj9SgSQdP07hbY0uUN+M4s5XuQOvZRbffCUo
uHkr37s+fEDRTVzTVHM0yZWY9za3tadAWNYDx6HrS5m3eRA=
X-Google-Smtp-Source: AMsMyM4wXSQZXptSikRg6shw0WuTmu4s4c4P108rflsiyxjgpNI4w7iH0ukJeVECgmfylk236m4QKd8CGkpe+81+M4c=
X-Received: by 2002:a2e:9d8a:0:b0:26e:314c:c3fd with SMTP id
c10-20020a2e9d8a000000b0026e314cc3fdmr8831256ljj.105.1665542793796; Tue, 11
Oct 2022 19:46:33 -0700 (PDT)
In-Reply-To: <10d193c8-0dbe-41c8-9c63-17e4c04f1c97n@googlegroups.com>
X-Content-Filtered-By: Mailman/MimeDel 2.1.39
X-BeenThere: python-list@python.org
X-Mailman-Version: 2.1.39
Precedence: list
List-Id: General discussion list for the Python programming language
<python-list.python.org>
List-Unsubscribe: <https://mail.python.org/mailman/options/python-list>,
<mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive: <https://mail.python.org/pipermail/python-list/>
List-Post: <mailto:python-list@python.org>
List-Help: <mailto:python-list-request@python.org?subject=help>
List-Subscribe: <https://mail.python.org/mailman/listinfo/python-list>,
<mailto:python-list-request@python.org?subject=subscribe>
X-Mailman-Original-Message-ID: <CAGGBd_ogqpaosQ0x=DnN34Gx-8Nb4iB9aktUD1oMPA2r7oAUyA@mail.gmail.com>
X-Mailman-Original-References: <10d193c8-0dbe-41c8-9c63-17e4c04f1c97n@googlegroups.com>
 by: Dan Stromberg - Wed, 12 Oct 2022 02:46 UTC

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/

Re: flattening lists

<mailman.670.1665593143.20444.python-list@python.org>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=19843&group=comp.lang.python#19843

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From: antoon.p...@vub.be (Antoon Pardon)
Newsgroups: comp.lang.python
Subject: Re: flattening lists
Date: Wed, 12 Oct 2022 18:45:39 +0200
Lines: 18
Message-ID: <mailman.670.1665593143.20444.python-list@python.org>
References: <10d193c8-0dbe-41c8-9c63-17e4c04f1c97n@googlegroups.com>
<0ee80123-5f94-ac3f-3384-d9441c5a69c5@vub.be>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
X-Trace: news.uni-berlin.de EdShiCLdXtf8fco7lzzPkAQ2yIF5bjnpnermlY25RZJQ==
Return-Path: <Antoon.Pardon@vub.be>
X-Original-To: python-list@python.org
Delivered-To: python-list@mail.python.org
Authentication-Results: mail.python.org; dkim=pass
reason="1024-bit key; unprotected key"
header.d=vub.be header.i=@vub.be header.b=oJq3fgGa; dkim-adsp=pass;
dkim-atps=neutral
X-Spam-Status: OK 0.013
X-Spam-Evidence: '*H*': 0.97; '*S*': 0.00; 'python:': 0.05; 'e.g.':
0.07; 'received:eur05-am6-obe.outbound.protection.outlook.com':
0.07; 'skip:[ 20': 0.09; 'way?': 0.09; 'antoon': 0.16;
'from:addr:antoon.pardon': 0.16; 'from:addr:vub.be': 0.16;
'from:name:antoon pardon': 0.16; 'message-id:@vub.be': 0.16;
'pardon': 0.16; 'received:40.107.22': 0.16; 'schreef': 0.16;
'subject:lists': 0.16; 'tedious': 0.16; 'turns': 0.16; 'to:addr
:python-list': 0.20; 'python,': 0.25; 'anyone': 0.25; 'else':
0.27; '>>>': 0.28; 'header:User-Agent:1': 0.30; 'think': 0.32;
'there': 0.33; 'mean': 0.34; 'header:In-Reply-To:1': 0.34;
'built': 0.36; 'lists': 0.37; "it's": 0.37; 'quite': 0.39; 'list':
0.39; 'something': 0.40; 'should': 0.40; 'skip:o 10': 0.61;
'becomes': 0.64; 'in.': 0.64; 'exactly': 0.68; 'order': 0.69;
'times': 0.69; 'depending': 0.70; 'easy': 0.74; 'out.': 0.80;
'received:eurprd01.prod.exchangelabs.com': 0.84
ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none;
b=hZUnnGwtMYoGTs0otFcLPhwGI++WpMv2detrPPrDL9ffTyOKqeJmJJioY2ESvLEyXfmAQqjV9bzQ1op4+gVIFPrePHL2KeddyeCAWjKtyJe7VNzdqvzmHry6ua4zMgsafisNfJmUtIWr08hERuONe6+wxKfZSwmG6U420QVyH7T1Cm9W9kQn1HRNXXN5RDAG9uGupRxICTG/ZB1ExtSSlqesFRQVmi1MQcGffxEoNER43qcGHWlVR5TMp7Pdl/6r5SFSAUJFZOtiAhpUbo3Plpzgfm+nUI9ea3qYQEwoj4jUNGFzCYlsJ5nZyQ1zLo2XbFr5A9g3+aIgF/pE0iK+jQ==
ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com;
s=arcselector9901;
h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1;
bh=WpCVmQ+kvZ5GuKksHqe1K4Nfzf3G7qwnIXCA0ZTs52w=;
b=YQtwMO4Ol3CF5TlYyyCpZftbF4rKVKjL1pHkbtWS8eEtWJOy6mPGuee+Yx2N5fG0AElxP8BgyTl+KVw+eXByVOvzzaB+8+4Hem1KpbCrJila4G+xvF0OubS4zozmf2wVgRjulq6R0qIL4aio4ZovwGB7ETIvvLgkVClPiCMxMlMH5CEF6eo1MF7T/wC+ljirfH0nAim7ngdHNgH4UdpbW3xuJsvHhIr6gddnnexOxFbosgILWRKuAcdrV2crGDd4/bRaHRffl/sjvfSyifBB+58qpUprOOIYh9pAsNj+qWKKWSH0lh9YUdE7rDgQr5NWh5rNKTua1OeO+JG1m+REkw==
ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass
smtp.mailfrom=vub.be; dmarc=pass action=none header.from=vub.be; dkim=pass
header.d=vub.be; arc=none
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=vub.be; s=selector1;
h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck;
bh=WpCVmQ+kvZ5GuKksHqe1K4Nfzf3G7qwnIXCA0ZTs52w=;
b=oJq3fgGakd3migssOekJHvC44FIeRP001vqym9GojmvSYlperMMHDBMbamn1uecppDlEu5uf40wesPqZXAGYlLc2GHHEjnJIAvcNLMP5PtxjJgToAQC4zEEB7DhHfOECnPYZHMavce3+CljbnZ24VE8yytaRlFAtd4mY26xJBBU=
Authentication-Results: dkim=none (message not signed)
header.d=none;dmarc=none action=none header.from=vub.be;
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101
Thunderbird/102.2.2
Content-Language: nl-BE
In-Reply-To: <10d193c8-0dbe-41c8-9c63-17e4c04f1c97n@googlegroups.com>
X-ClientProxiedBy: AM0PR04CA0080.eurprd04.prod.outlook.com
(2603:10a6:208:be::21) To AM0PR01MB4147.eurprd01.prod.exchangelabs.com
(2603:10a6:208:6a::12)
X-MS-PublicTrafficType: Email
X-MS-TrafficTypeDiagnostic: AM0PR01MB4147:EE_|PA4PR01MB9474:EE_
X-MS-Office365-Filtering-Correlation-Id: 7e327798-5ee4-4b8f-4a9b-08daac7132c1
X-MS-Exchange-SenderADCheck: 1
X-MS-Exchange-AntiSpam-Relay: 0
X-Microsoft-Antispam: BCL:0;
X-Microsoft-Antispam-Message-Info: 9wYqJvmXq4sGDrsej3HCX6nC9P5NLWttDmvG0v+E9g/bK8izsQ/7aYNghEHqd9ijyQb1faKHhg+svNrQApQhFsh46n2D0TYeb9M+kFOTnKIxGo7uJ7Uz+5f8BI0yS43fac2qWRO4MWlTZSki0ov83jC30tsB/7JW8WHQjD26CEe2oc6KixZWLxUG6SsZbUzve3lVM33CdpttTEvxWbGgWJmTy0GJ92LNJlFspqEPX9H+pw1wCkgLJhs4Qoaf3QvjBMnurllocdofcQ5rv5006PQ2d3KVnpFVIlzbLjVQeUrXTHAq+eXBJdtGloOxlGVllyM4NIU5HfroG8CIsedW06ewTiT9ndOsGQjT0sEsCOyVPdRPYMzBjyU+J/2zNFM/VnXP4b+NX4lafsj3/BpxFa7o+/hCNWH9lyTsFe5Z6BXMyuWo3J//mNMswQ4LvQc42gAZ5aaQj6BBXlAH/k18I7vZMfOxSR6ddKJWgM6Jdi9mSj5P/TXTB0/S7Wl7iZnElRn2caAZmoNevNZxOLIMTyGswb+L6Kcm0ltbNXifr95CZ8LZ5GGG6RBi0EJ80N7IGz71ZVHk7UZVaE/uDMdkqHQwZZ2mEaNA4i1QfLfcgbNsqC2PbeJ6oFHNIaAecOhz242qb9fEWZunOBxyAtmoZrQj8PKacyzmwQ4ewnZpO80B0dE7fD9hOO2bd1kH5q6/puk6ff68GKfV7/fqMwjsbdZ0UvgQj9/xo9viMg57yVGb1mqnTLt1aeZpgbA6ZSUn
X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:;
IPV:NLI; SFV:NSPM; H:AM0PR01MB4147.eurprd01.prod.exchangelabs.com; PTR:;
CAT:NONE;
SFS:(13230022)(4636009)(346002)(366004)(376002)(39860400002)(396003)(136003)(451199015)(2616005)(31696002)(186003)(5660300002)(66946007)(38100700002)(86362001)(4744005)(44832011)(3480700007)(41300700001)(7116003)(8936002)(786003)(41320700001)(316002)(478600001)(66476007)(6916009)(66556008)(83380400001)(6512007)(6486002)(2906002)(8676002)(6506007)(31686004)(36756003)(43740500002);
DIR:OUT; SFP:1101;
X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1
X-MS-Exchange-AntiSpam-MessageData-0: oeheOTVfz6BxRExbv259ULVn0DRcf0FOKAM1NoCgY7EhN
ldIGw+Dwo/WPGKpxr7tF5nCDPdS3HkNXcQtwpMGc2PgGP
qWLTKsYZlz/Hn1/oe4IHe50YfiCn4K+RMQpRealJ6ozFe
+ja6Oc4y3hXRjBh//MKI3RfVepZLvAhjUJoHk3AUZJThh
4TVa5VefHVaVWucW6f67FYYin3TmozwFnoFYfiUc2rj/Z
7BDmOXIe5KchEfs2Da4+6tXvSvZWMxuyHiZlZFs3NTRpV
lk8zsdMiflk6imajGjxeVHC/dCA7mrQYeASml98lGggjB
XR9sUyk56IntMTpv5yE3dFRHwaLBkhzAVAvyU4mzEvlmB
6BBqKCDXrI3/6tcUKsiAvzq5nA7OR4nsT1XhKh1CfHcwE
JRUDlBbn768FJsjUL6Np6Rn9dPc6M6ZQKqKq9hMfkiHaP
8Y3gG4d+yCZ380mOccZfH7Jz389Z/0TfgPU5YU7XjfpsQ
4jHHQAs1dtY2BaovZZpISly2Ng95ySVaHMxR6rFmzfrK7
dZReeVqdUGx3E2evOwwoFT7A1TeQa47FuOIJUT/z6Mus0
ZazY7xhQD4xaALrVvUF51IqMX6n0J8o7FsvT/IdP/hDQV
LVejfTapRZf6xTK2d1xuCT2osPwmci2GSvcLt4C8vrIz9
YXK7b6YrhNtG/7/gMMdykczCzvKH32sZ4H+SmVGfh4GWK
jakMAUUVvA9LHSSidFVwv04cpyEjzqgin880HkeQSH8hK
JZ7xxeeZKLDcIR36P6zWZP/VTa872nNtariAgi01dXEPZ
Ck6J2UiYjbhHMZwmwjclUYmtQP8g2LbI6NP8c2tn6jFL7
9Vlf2ynhhHyqNIOshjPJqxRBo/2gidl57h9Do/hmrdxvt
eFKztWyWGrCFac9zu/F7bG7vC/CtoFZ36gJ8g1Y7rrlGz
6oP3WaedaeDxAw6iukbt1RPvrPKVuuYER6o7Bjrvm/0Ix
ptBYPy9dC0s1ncVilXwnU5Hv4K9xnfv51QGFFfAQM0PZK
7AhADNmryuqffrEecxKoDEiq6cVOA4Jh3ZzwILP6+F8Yv
yT1/r2QKk3DUXhfIzDi4cdHiFnRFYWF+6+r9KQCOrRgc0
YJmc2WJoTuEvxCo0349lFw2BpuYFuboA/xrrRVdIfwelz
HG+SVKCjhNrZa1IYso+06QLjjk4sWNBNvrAhs48SkuJMJ
WVNQ4CKhAtrkjjjLJoaNcxhKLsz37uh2Vthca9OlhLfw3
31eKMBxFhyLL8MBEB3gEahimZhxDN4np0+qpFiBxdRWsK
+dobvTEA2iVIhCm0jFgPZfUWZdivSZbeo7ec2kGV3Uq2k
7JGk7CI36uHPwMijXFD9ie524w2m2CuvBbjshUCkVjWgN
9oEpkUo76a5rphv1jGDlDqR28aXxiqBqHw52UKALNqNUM
o52Eo8Ic+GmlPzbcaDybrlM70H+Ck9/xCmwUJah4NmOMx
o1nWQiylTPkv+oasKfnzoBZz1n87zkii9D5X4e92kFckZ
tzgKX1DchPHNn0pCkz5xdqBOBv3Xx6kuQYNtto5gWrcH/
GTfHbMWBm+aVfhTXihJ4vfHOUFWjRtEhTnNEKic8WVzxq
cgVOaaxORVSce5d4l9TguT2wpIU93Eu4idDdjbQXIlOq
X-OriginatorOrg: vub.be
X-MS-Exchange-CrossTenant-Network-Message-Id: 7e327798-5ee4-4b8f-4a9b-08daac7132c1
X-MS-Exchange-CrossTenant-AuthSource: AM0PR01MB4147.eurprd01.prod.exchangelabs.com
X-MS-Exchange-CrossTenant-AuthAs: Internal
X-MS-Exchange-CrossTenant-OriginalArrivalTime: 12 Oct 2022 16:45:41.0875 (UTC)
X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted
X-MS-Exchange-CrossTenant-Id: 695b7ca8-2da8-4545-a2da-42d03784e585
X-MS-Exchange-CrossTenant-MailboxType: HOSTED
X-MS-Exchange-CrossTenant-UserPrincipalName: xUgqyJmRGlaCaXhOBAE4S7Dmpt/OdDLY75b6KyxqgNNuBJFHwH1ZJ+qaWBo5SPU3fvi6abyyC6hO4ygboni2Qw==
X-MS-Exchange-Transport-CrossTenantHeadersStamped: PA4PR01MB9474
X-BeenThere: python-list@python.org
X-Mailman-Version: 2.1.39
Precedence: list
List-Id: General discussion list for the Python programming language
<python-list.python.org>
List-Unsubscribe: <https://mail.python.org/mailman/options/python-list>,
<mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive: <https://mail.python.org/pipermail/python-list/>
List-Post: <mailto:python-list@python.org>
List-Help: <mailto:python-list-request@python.org?subject=help>
List-Subscribe: <https://mail.python.org/mailman/listinfo/python-list>,
<mailto:python-list-request@python.org?subject=subscribe>
X-Mailman-Original-Message-ID: <0ee80123-5f94-ac3f-3384-d9441c5a69c5@vub.be>
X-Mailman-Original-References: <10d193c8-0dbe-41c8-9c63-17e4c04f1c97n@googlegroups.com>
 by: Antoon Pardon - Wed, 12 Oct 2022 16:45 UTC

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

1
server_pubkey.txt

rocksolid light 0.9.81
clearnet tor