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
  Nested Dictionaries, good programming practice? Curbie 6 2,136 May-28-2025, 10:45 PM
Last Post: Curbie
  Mirroring disk structures in nested dictionaries Curbie 16 4,656 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 2,843 Oct-13-2024, 01:28 PM
Last Post: Pedroski55
  Nested Lists & Dictionaries Hudjefa 5 2,261 Sep-23-2024, 08:20 PM
Last Post: DeaD_EyE
  sort list of dictionaries by value jacksfrustration 4 2,574 Jun-21-2024, 08:44 PM
Last Post: deanhystad
  Sort a list of dictionaries by the only dictionary key Calab 2 2,290 Apr-29-2024, 04:38 PM
Last Post: Calab
  Access list of dictionaries britesc 4 4,913 Jul-26-2023, 05:00 AM
Last Post: Pedroski55
  List all possibilities of a nested-list by flattened lists sparkt 1 2,293 Feb-23-2023, 02:21 PM
Last Post: sparkt
  Updating nested dict list keys tbaror 2 2,516 Feb-09-2022, 09:37 AM
Last Post: tbaror
  Python Program to Find the Total Sum of a Nested List vlearner 8 10,790 Jan-23-2022, 07:20 PM
Last Post: menator01

Forum Jump:

User Panel Messages

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