Python Forum
Help to flatten list of nested dictionaries - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Help to flatten list of nested dictionaries (/thread-16351.html)



Help to flatten list of nested dictionaries - shawbapmp - Feb-24-2019

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?


RE: Help to flatten list of nested dictionaries - Larz60+ - Feb-24-2019

Give it a go, and show what you have tried.


RE: Help to flatten list of nested dictionaries - shawbapmp - Feb-25-2019

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?


RE: Help to flatten list of nested dictionaries - Larz60+ - Feb-25-2019

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



RE: Help to flatten list of nested dictionaries - shawbapmp - Feb-25-2019

Holy elegant code, Batman!
That sure beats my loopy mess.
Much appreciated Larz60!
Cheers.