0

I have a nested dictionary called 'data' that has keys (dictionary name) and values (dictionary).

I wish to separate out the individual dictionaries from the outer dictionary.

This is how I read a folder of csv files to make the dict of dicts:

#read csvs with filename as dictionary names
data= dict()
for file in files:
    key = file
    val = pd.read_csv(
            csv_path + file + ".csv", index_col=0).to_dict(orient='dict')['values']
    data[key] = val
martineau
  • 119,623
  • 25
  • 170
  • 301

1 Answers1

0

They could be unpacked like this:

master = {'dic1': {'foo': 1, 'bar': 42}, 'dic2': {'sas': 3, 'baz': 13}}

d1, d2 = master.values()
print(d1)  # -> {'foo': 1, 'bar': 42}
print(d2)  # -> {'sas': 3, 'baz': 13}
martineau
  • 119,623
  • 25
  • 170
  • 301
  • is there a way to do this as a loop. there are hundreds of dictionaries to unpack. i would like their name to be the key that was associated with it –  Oct 22 '19 at 23:58
  • 1
    Do you mean you want to create _separate variables_ whose named from the keys in the nested dictionary with the value associated with each of those keys? If not, how do you want them separated out into individual dictionaries? – martineau Oct 23 '19 at 01:13
  • yes that is what I mean. I want all those keys to be the name of the separate variables –  Oct 23 '19 at 01:25
  • 1
    In that case your question is a duplicate of [How do I create a variable number of variables?](https://stackoverflow.com/questions/1373164/how-do-i-create-a-variable-number-of-variables) — which essentially is that "no", that you don't want to do that. – martineau Oct 23 '19 at 01:29