[Solved] Flatten list of lists within dictionary values before processing in Pandas


As a follow up to the original post. I managed to resolve the issue, and flattened the lists within the dictionary, with the help of the following generator function:

Taken from here:

def flatten(l):
    for el in l:
        if isinstance(el, collections.Iterable) and not isinstance(el, basestring):
            for sub in flatten(el):
                yield sub
        else:
            yield el

And using it on the dictionary as follows gave the desired output:

asa = {k: list(flatten(v)) for k, v in asa.items()} 

Please note there is another version of this function for Python 3 that can be found with the link above.

solved Flatten list of lists within dictionary values before processing in Pandas