(Mar-22-2019, 07:10 PM)snippsat Wrote: Just to use yield from
That isn't as efficient as it may look, when compared to the solution with an explicit stack. In this elegant-looking solution, for deep nesting, Python will keep a stack of generators and when you want to get the "next" value you'll have to start at the "bottom" of the stack, work your way to the top, and then return the value back through that stack of generators.The explicit stack solution is linear time, but iterating over that generator would be more like quadratic (the true complexity is more involved, but I think "quadratic" describes the problem here well enough).
Also, the OP doesn't appear to need arbitrary nesting :)