Any quicker way to flatten a list of pairs?

Your two-line code is beautifully readable, but if you are playing code golf and need to cram everything into one line to save a few characters, you can write the ugly code:

[x for y in [[w**2, w**3] for w in lt] for x in y]

See also this proposal here:

https://mail.python.org/archives/list/python-ideas@python.org/message/7G732VMDWCRMWM4PKRG6ZMUKH7SUC7SH/

although unfortunately it doesn’t exist yet.

1 Like