silveira neto

carbon-based lifeform. virgo supercluster

Menu Close

Python, flatten a list

Surprisingly python doesn’t have a shortcut for flatten a list (more generally a list of lists of lists of…).

I made a simple implementation that doesn’t use recursion and tries to be written clearly.

I get a element from a “bad” list (a list that can have another lists). If the element is not a list we store in our flat list. If the element is still a list we deal with him later. The flat list always have elements that are not a list.
To preserve the original order we reverse the elements at the end.

© 2016 silveira neto. All rights reserved.

Theme by Anders Norén.