Python Forum
Help to flatten list of nested dictionaries
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help to flatten list of nested dictionaries
#1
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?
Reply
#2
Give it a go, and show what you have tried.
Reply
#3
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?
Reply
#4
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
Reply
#5
Holy elegant code, Batman!
That sure beats my loopy mess.
Much appreciated Larz60!
Cheers.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Mirroring disk structures in nested dictionaries Curbie 16 3,048 Apr-27-2025, 02:43 PM
Last Post: deanhystad
  What is a faster way to deep copy a nested list without using .deepcopy()? Shervin_Ataeian 1 1,704 Oct-13-2024, 01:28 PM
Last Post: Pedroski55
  Nested Lists & Dictionaries Hudjefa 5 1,478 Sep-23-2024, 08:20 PM
Last Post: DeaD_EyE
  sort list of dictionaries by value jacksfrustration 4 1,675 Jun-21-2024, 08:44 PM
Last Post: deanhystad
  Sort a list of dictionaries by the only dictionary key Calab 2 1,540 Apr-29-2024, 04:38 PM
Last Post: Calab
  Access list of dictionaries britesc 4 3,088 Jul-26-2023, 05:00 AM
Last Post: Pedroski55
  List all possibilities of a nested-list by flattened lists sparkt 1 1,850 Feb-23-2023, 02:21 PM
Last Post: sparkt
  Updating nested dict list keys tbaror 2 2,009 Feb-09-2022, 09:37 AM
Last Post: tbaror
  Python Program to Find the Total Sum of a Nested List vlearner 8 8,418 Jan-23-2022, 07:20 PM
Last Post: menator01
  Looping through nested elements and updating the original list Alex_James 3 3,004 Aug-19-2021, 12:05 PM
Last Post: Alex_James

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020