I am looking to write a recursive function:

```
arguments: d, dictionary

result: list of dictionaries

def expand_dictionary(d):
    return []

```

The function recursively goes through a dictionary and flattens nested objects using an \_, in addition it expands out nested lists into the array, and includes the parent label.

Think of creating a relational model from a document.

Here is an example input and output:

```
original_object = {
  "id" : 1,
  "name" : {
    "first" : "Alice",
    "last" : "Sample"
  },
  "cities" : [
    {
      "id" : 55,
      "name" : "New York"
    },
    {
      "id" : 60,
      "name" : "Chicago"
    }
  ],
  "teachers" : [
    {
      "id" : 2
      "name" : "Bob",
      "classes" : [
        {
          "id" : 13,
          "name" : "math"
        },
        {
          "id" : 16,
          "name" : "spanish"
        }
      ]
    }
  ]
}

expected_output = [
  {
    "id" : 1,
    "name_first" : "Alice",
    "name_last" : "Sample"
  },
  {
    "_parent_object" : "cities",
    "id" : 55,
    "name" : "New York"  
  },
  {
    "_parent_object" : "cities",
    "id" : 60,
    "name" : "Chicago"  
  },
  {
    "parent_object" :"teachers",
    "id" : 2,
    "name" : "Bob"
  },
  {
    "parent_object" :"teachers_classes",
    "id" : 13,
    "name" : "math"
  },
  {
    "parent_object" :"teachers_classes",
    "id" : 16,
    "name" : "spanish"
  }
]

```

the code currently being used for flattening is:

```
def flatten_dictionary(d):
  def expand(key, value):
      if isinstance(value, dict):
        return [ (key + '_' + k, v) for k, v in flatten_dictionary(value).items() ]
      else:
        #If value is null or empty array don't include it
        if value is None or value == [] or value == '':
          return []
        return [ (key, value) ]

  items = [ item for k, v in d.items() for item in expand(k, v) ]

  return dict(items)

```