One-liner to merge lists?

Chris Angelico rosuav at gmail.com
Tue Feb 22 23:42:04 EST 2022


On Wed, 23 Feb 2022 at 15:04, Dan Stromberg <drsalists at gmail.com> wrote:
>
> On Tue, Feb 22, 2022 at 7:46 AM David Raymond <David.Raymond at tomtom.com>
> wrote:
>
> > > Is there a simpler way?
> >
> > >>> d = {1: ['aaa', 'bbb', 'ccc'], 2: ['fff', 'ggg']}
> > >>> [a for b in d.values() for a in b]
> > ['aaa', 'bbb', 'ccc', 'fff', 'ggg']
> > >>>
> >
>
> I like that way best.
>
> But I'd still:
> 1) use a little more descriptive identifiers
> 2) put it in a function with a descriptive name
> 3) give the function a decent docstring

If you're going to do that, then you may as well use list(chain()) and
let the work be done in C.

ChrisA


More information about the Python-list mailing list