flatten(nested) that returns a single flat list of values from a list that may contain other lists nested to any depth, using recursion.flatten([1, [2, [3, 4], 5], 6])
[1, 2, 3, 4, 5, 6]
Every nested list is recursively expanded in order.
flatten([[1], [2, [3]], []])
[1, 2, 3]
Empty sublists contribute nothing.
For each item, check isinstance(item, list).
If it is a list, extend the result with flatten(item); otherwise append it directly.