title: Python: convert types in deeply nested dictionary or array
body: <p>I have written the following helper function to convert ObjectIds (a bson type) to strings.  The function should work for deeply nested dictionaries, arrays, and combinations of the two.</p>
  
  <p>The current function is below:</p>
  
  <pre><code>from datetime import datetime
  from bson import ObjectId
  
  def clean_dict_helper(d):
      if not d:
          return None
      if isinstance(d, list):  # For those db functions which return list
          if len(d) == 0:
              return []
          return [clean_dict_helper(x) for x in d]
      for k, v in d.items():
          if isinstance(v, dict):
              v = clean_dict_helper(v)
              d.update({k: v})
          else:
              if isinstance(v, ObjectId):
                  v = str(v)
              elif isinstance(v, datetime):
                  v = str(v)
              d.update({k: v})
      return d
  
  
  test_case = {'id': bson.ObjectId("5e126ddf276ab18820e00ddf"), 'arr': [{'value': 4, 'id': bson.ObjectId("5e126ddf276ab18820e00ddf")}]}
  
  result = clean_dict_helper(test_case)
  
  print(result)
  &gt;&gt;&gt; {'arr': [{'id': ObjectId('5e126ddf276ab18820e00ddf'), 'value': 4}], 'id': '5e126ddf276ab18820e00ddf'}
  
  </code></pre>
  
  <p>However it seems to fail for objects inside of arrays inside of Objects.  I'm wondering if there is a better way to iterate through nested iterables and convert types?</p>