1

I want to flatten a multi-level dictionary into a single level dictionary. The new single levels keys should be concatenated with a separator "_". It's not a duplicate of the other flatten dict question since this has arrays that are concatenated into a string properly separated.

Input data:

{'dates': [
    {'date': '1970-08-01',   'type': 'Date of birth'},
    {'unparsed_date': '1987','type': 'Year of birth'},
    {'date': '2000-00-00',   'type': 'Date of birth2'}
]}

Expected output:

{
 'dates_date': '1970-08-01;; 2000-00-00',
 'dates_type': 'Date of birth; Year of birth; Date of birth2',
 'dates_unparsed_date': ';1987'
}

When turned into a table:
enter image description here

I've wrote a 200 line function which I think is spaghetti and I won't post here, but my output was like this (no way to fix other than to rewrite the entire function):
Actual (wrong) output:

{
 'dates_date': '1970-08-01; 2000-00-00',
 'dates_type': 'Date of birth; Year of birth; Date of birth2',
 'dates_unparsed_date': ';1987'
}
CDJB
  • 14,043
  • 5
  • 29
  • 55
KayleMaster
  • 331
  • 2
  • 13
  • Does this answer your question? [Flatten nested dictionaries, compressing keys](https://stackoverflow.com/questions/6027558/flatten-nested-dictionaries-compressing-keys) – Ashutosh Jan 08 '20 at 09:26

1 Answers1

1

The following should work:

Code:

d_new = dict()

for k, v in d.items():
    for k2 in set(k for d in v for k in d.keys()):
        d_new[f"{k}_{k2}"] = '; '.join(d2.get(k2, '') for d2 in v)

Output:

>>> d_new
{'dates_unparsed_date': '; 1987; ',
 'dates_date': '1970-08-01; ; 2000-00-00',
 'dates_type': 'Date of birth; Year of birth; Date of birth2'}

Or, as a one-line dictionary comprehension:

>>> {f"{k}_{k2}" : '; '.join(d2.get(k2, '') for d2 in v) for k, v in d.items() for k2 in set(k for d in v for k in d.keys())}
{'dates_unparsed_date': '; 1987; ',
 'dates_date': '1970-08-01; ; 2000-00-00',
 'dates_type': 'Date of birth; Year of birth; Date of birth2'}
CDJB
  • 14,043
  • 5
  • 29
  • 55