title: Python3 sorting lists of nested dicts with lambda
body: <p>I have a deep nested object of various lists and dicts that I retrieve as json which I need to compare to another version of itself. The issue is that all lists are basically unsorted, therefore I need to sort before comparing them. Any deep diff library I've tried failed without proper sorting the dicts position in the lists, so here we go.</p>
  
  <p>Sample object that requires sorting:</p>
  
  <pre><code>{
      "main":{
          "key1":"value1",
          "key2":"value2",
          "key3":[{
              "sub1":"value2",
              "sub2":{
                  "subsub":[{
                      "subsubsub1":10,
                      "subsubsub2":11,
                      "subsubsub3":[10,11,12]
                  },{
                      "subsubsub1":7,
                      "subsubsub2":8,
                      "subsubsub3":[9,7,8]
                  }]
              }
          },{
              "sub1":"value1",
              "sub2":{
                  "subsub":[{
                      "subsubsub1":1,
                      "subsubsub2":2,
                      "subsubsub3":[1,2,3]
                  },
                  {
                      "subsubsub1":4,
                      "subsubsub2":5,
                      "subsubsub3":[5,6,4]
                  }]
              }
          }]
      }
  }
  </code></pre>
  
  <p>Besides a few recursive loops I'm trying to sort the dicts by translating them with sorted lists into sorted tuples and hash them.</p>
  
  <p><strong><em>Edit:</em></strong>
  The object is passed into unnest()</p>
  
  <pre><code>    def unnest(d):
      for k, v in d.items():
          if isinstance(v, dict):
              d.update({k: unnest(v)})
          elif isinstance(v, list):
              d.update({k: unsort(v)})
      return d
  
  
  def unsort(l):
      for i, e in enumerate(l):
          if isinstance(e, dict):
              l[i] = unnest(e)
          elif isinstance(e, list):
              l[i] = unsort(e)
      return sorted(l, key=lambda i: sort_hash(i))
  
  
  def unnest_hash(d):
      for k, v in d.items():
          if isinstance(v, dict):
              d.update({k: unnest_hash(v)})
          elif isinstance(v, list):
              d.update({k: sort_hash(v)})
      return hash(tuple(sorted(d.items())))
  
  
  def sort_hash(l):
      if isinstance(l, list):
          for i, e in enumerate(l):
              if isinstance(e, dict):
                  l[i] = unnest_hash(e)
              elif isinstance(e, list):
                  l[i] = sort_hash(e)
          return hash(tuple(sorted(l)))
      elif isinstance(l, dict):
          return unnest_hash(l)
      else:
          return hash(l)
  </code></pre>
  
  <p>However for some reason the hash value gets written into the "sorted" list:</p>
  
  <pre><code>{'main': {'key1': 'value1', 'key2': 'value2', 'key3': [{'sub1': 'value2', 'sub2': -4046234112924644199}, {'sub1': 'value1', 'sub2': 4015568797712784641}]}}
  </code></pre>
  
  <p>How can I prevent the sort value in the lambda function to be written into the returned sorted list?
  Thanks!</p>