[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

How to expand and flatten a nested of list of dictionaries of varied lengths?

On 10/18/20 2:09 PM, Shaozhong SHI wrote:
> Even worse is that, in some cases, an addition called serviceRatings as a
> key occur with new data unexpectedly.
> How to produce a robust Python/Panda script to coping with all these?
> Regards,
> David
> u'historicRatings': [{u'overall': {u'keyQuestionRatings': [{u'name':
> u'Safe', u'rating': u'Requires improvement'}, {u'name': u'Well-led',
> u'rating': u'Requires improvement'}], u'rating': u'Requires improvement'},

truncating this to avoid a huge paste...

can you give an example of what it would look like "flattened"?

list flattening is pretty straightforward, and even has a standard
library method, itertools.chain, to help out.

to flatten a dict though, you have to deal with what would happen if the
keys aren't unique. if you look at the above, for example, there are two
places where there's a key 'rating' though they're at different levels
in the data structure, so you have to be clear on what should happen here.