Python Forum

Full Version: Help to flatten list of nested dictionaries
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi Py Developers,
I'm a Python 3 newbie with problems trying to flatten a list of nested dictionaries. It is Oanda API candle data that looks like this:
[{'mid': {'h': '1.31', 'o': '1.32', 'c': '1.33', 'l': '1.34'}, 'volume': 99, 'time': '2019-02-22T21', 'complete': False}]
For each 'get' of the data, the order of the list changes, so it might start with the 'time' item. I only want to extract the candle high and low, i.e the 'h' and 'l' dictionary values.
I've tried some Googled functions that iterate through the nested dictionaries, but none of them work for me.
Can anybody help me?
Give it a go, and show what you have tried.
I'm new to this forum and I don't know how to insert those nice code snippet boxes, but here is what I tried. It works to flatten the dictionaries, but only if there are no [ and ] brackets around the nested dictionaries, which is the way the API data is retrieved.

d = {'mid': {'h': '1.31', 'o': '1.32', 'c': '1.33', 'l': '1.34'}, 'volume': 99, 'time': '2019-02-22T21', 'complete': False}
def flatten(d):
    def items():
          for key, value in d.items():
               if isinstance(value, dict):
                    for subkey, subvalue in flatten(value).items():
                         yield key + "." + subkey, subvalue
               else:
                    yield key, value
    return dict(items())
dictout = flatten(d)
hi = dictout.get('mid.h')
lo = dictout.get('mid.l')
print("Hi= ",hi," and Lo= ",lo)
So like I say, this works fine on the dictionary line at the top, but the actual API line is a List with opening and closing []. I don't know how to get around that. Suggestions?
rawdata = [{'mid': {'h': '1.31', 'o': '1.32', 'c': '1.33', 'l': '1.34'}, 'volume': 99, 'time': '2019-02-22T21', 'complete': False}]

d = rawdata[0]
print(d)
h = d['mid']['h']
l = d['mid']['l']
print(f'h: {h}, l: {l}')
output:
Output:
{'mid': {'h': '1.31', 'o': '1.32', 'c': '1.33', 'l': '1.34'}, 'volume': 99, 'time': '2019-02-22T21', 'complete': False} h: 1.31, l: 1.34
Holy elegant code, Batman!
That sure beats my loopy mess.
Much appreciated Larz60!
Cheers.