A Python function for flattening irregular list of lists
Here’s a nice short function for flattening irregular lists in Python that I found on Stackoverflow a few weeks back. It returns a generator so should work fine for very big lists.
def flatten_list(object_list):
"""
Flattens a list of objects.
"""
for element in object_list:
if isinstance(element, collections.Iterable) and not \
isinstance(element, basestring):
for sublist in flatten_list(element):
yield sublist
else:
yield elementI tested it with lists like the following and it did the job: …
