One-liner to merge lists?

Chris Angelico rosuav at gmail.com
Fri Feb 25 18:11:30 EST 2022


On Sat, 26 Feb 2022 at 10:05, Albert-Jan Roskam <sjeik_appie at hotmail.com> wrote:
>    Was also thinking about reduce, though this one uses a dunder method:
>    from functools import reduce
>    d = {1: ['aaa', 'bbb', 'ccc'], 2: ['fff', 'ggg']}
>    print(reduce(list.__add__, list(d.values())))

If you don't want to use the dunder, just use the operator module:

reduce(operator.add, d.values())

But ultimately, that's still the same as sum(), and it's no more
readable than chain(), so I'd still be inclined to go with chain and
then wrap it in a function if you need the clarity.

ChrisA


More information about the Python-list mailing list