Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

5
  • 3
    great solution, however would be much helpful if you added some comment to describe what e, a, n refer to Commented Nov 20, 2013 at 14:16
  • 3
    @WolfgangKuehne: Try args for n, intermediate (or the shorter mid or you might prefer element) for a and result for e, so: flatten = lambda *args: (result for mid in args for result in (flatten(*mid) if isinstance(mid, (tuple, list)) else (mid,))) Commented Jan 5, 2014 at 21:18
  • This is significantly faster than compiler.ast.flatten. Great, compact code, works for any (I think) object type. Commented Jul 30, 2015 at 23:04
  • This is the only solution I have come across, in a moderate google search, on any website that actually works for lists nested deeper than one level. Commented Aug 3, 2020 at 14:56
  • 1
    This is a work of art. So few characters, and still nearly impossible to understand. 10/10 best Python code golf I've seen yet 🏌️‍♂️🏌️‍♀️⛳️. Having something this short almost makes up for the fact that Python doesn't have a built-in flatten function. Commented Oct 26, 2020 at 2:07