title: How to transform this python loop into a list comprehension?
body: <p><strong>Nevermind. As indicated by  juanpa.arrivillaga, list comprehension does not return a dictionary &gt;&lt;</strong></p>
  <h1>Context</h1>
  <p>I have the following python code (see next piece of code). I would like to try and optimize it to compare the execution time between regular loop and list comprehension.</p>
  <h1>Regular loop version</h1>
  <pre><code>def flatten_json( nested_dict, flattened_dict={}, superior_level_key: str = &quot;&quot;):
  
      for key, value in nested_dict.items():
          if type(nested_dict[key]) is dict:
              flattened_dict = flatten_json(
                  nested_dict[key], flattened_dict, &quot;{}_&quot;.format(key))
          else:
              flattened_dict['{}{}'.format(superior_level_key, key)] = value
  
      return flattened_dict
  
  
  import json
  
  with open('json.json') as j:
      d = json.load(j)
      print(flatten_json(d, {}, &quot;&quot;))
  </code></pre>
  <h1>Current, failing, list comprehension version</h1>
  <pre><code>def flatten_json(nested_dict, flattened_dict={}, superior_level_key: str = &quot;&quot;):
  
      return [flatten_json(nested_dict[key], flattened_dict, &quot;{}_&quot;.format(key))
              if type(nested_dict) is dict
              else value for key, value in nested_dict.items()]
  
  
  import json
  
  with open('json.json') as j:
      d = json.load(j)
      print(flatten_json(d, {}, &quot;&quot;))
  </code></pre>
  <h1>Error</h1>
  <p>The list comprehension version throws the following error:</p>
  <pre><code>Traceback (most recent call last):
    File &quot;p1.py&quot;, line 13, in &lt;module&gt;
      print(flatten_json(d, {}, &quot;&quot;))
    File &quot;p1.py&quot;, line 3, in flatten_json
      return [flatten_json(nested_dict[key], flattened_dict, &quot;{}_&quot;.format(key))
    File &quot;p1.py&quot;, line 3, in &lt;listcomp&gt;
      return [flatten_json(nested_dict[key], flattened_dict, &quot;{}_&quot;.format(key))
    File &quot;p1.py&quot;, line 5, in flatten_json
      else value for key, value in nested_dict.items()]
  AttributeError: 'float' object has no attribute 'items'
  </code></pre>
  <h1>Question</h1>
  <p>Why is it throwing that error and how to fix it ?</p>
  <h1>Input</h1>
  <pre><code>{
      &quot;_score&quot;: 1.0,
      &quot;_index&quot;: &quot;sirene_prod&quot;,
      &quot;_id&quot;: &quot;AXSp612eur2DngRir4BH&quot;,
      &quot;_type&quot;: &quot;sirene_prod&quot;,
      &quot;_source&quot;: {
          &quot;enseigne&quot;: &quot;&quot;,
          &quot;codpos&quot;: {
              &quot;cp&quot;: &quot;17300&quot;,
              &quot;bur_distrib&quot;: &quot;300&quot;,
              &quot;depet&quot;: &quot;17&quot;
          },
          &quot;id&quot;: &quot;ddf9e5b2aa0099ff6934a3d83b1678f64e27859e377362ef8682a9b1&quot;,
          &quot;l3_normalisee&quot;: &quot;&quot;,
          &quot;apet700&quot;: &quot;10.71C&quot;,
          &quot;sigle&quot;: &quot;&quot;,
          &quot;siren&quot;: &quot;793120569&quot;,
          &quot;libapen&quot;: &quot;Boulangerie et boulangerie-patisserie&quot;,
          &quot;apen700&quot;: &quot;10.71C&quot;,
          &quot;cedex&quot;: &quot;&quot;,
          &quot;typvoie&quot;: &quot;AV&quot;,
          &quot;numvoie&quot;: 33,
          &quot;nom&quot;: &quot;&quot;,
          &quot;depet_limit&quot;: [
              &quot;16&quot;,
              &quot;24&quot;,
              &quot;33&quot;,
              &quot;79&quot;,
              &quot;85&quot;
          ],
          &quot;libcom&quot;: &quot;ROCHEFORT&quot;,
          &quot;l2_normalisee&quot;: {
              &quot;text&quot;: &quot;&quot;,
              &quot;nom&quot;: &quot;&quot;,
              &quot;initial&quot;: &quot;&quot;
          },
          &quot;libvoie&quot;: &quot;GAMBETTA&quot;,
          &quot;nic&quot;: &quot;00017&quot;,
          &quot;prenom&quot;: &quot;&quot;,
          &quot;nomen_long&quot;: {
              &quot;text&quot;: &quot;LA PASSION DU PAIN&quot;,
              &quot;nom&quot;: &quot;LA PASSION DU PAIN&quot;,
              &quot;initial&quot;: &quot;LPDP&quot;
          },
          &quot;indrep&quot;: &quot;&quot;
      }
  }
  </code></pre>
  <h1>Output</h1>
  <pre><code>{'_score': 1.0, '_index': 'sirene_prod', '_id': 'AXSp612eur2DngRir4BH', '_type': 'sirene_prod', '_surce_enseigne': '', 'codpos_cp': '17300', 'codpos_bur_distrib': '300', 'codpos_depet': '17', '_soure_id': 'ddf9e5b2aa0099ff6934a3d83b1678f64e27859e377362ef8682a9b1', '_source_l3_normalisee': '', '_surce_apet700': '10.71C', '_source_sigle': '', '_source_siren': '793120569', '_source_libapen': 'Bouangerie et boulangerie-patisserie', '_source_apen700': '10.71C', '_source_cedex': '', '_source_typvie': 'AV', '_source_numvoie': 33, '_source_nom': '', '_source_depet_limit': ['16', '24', '33', '79' '85'], '_source_libcom': 'ROCHEFORT', 'l2_normalisee_text': '', 'l2_normalisee_nom': '', 'l2_normamalisee_initial': '', '_source_libvoie': 'GAMBETTA', '_source_nic': '00017', '_source_prenom': '', renom': '', 'nomen_long_text': 'LA PASSION DU PAIN', 'nomen_long_nom': 'LA PASSION DU P_long_initiaAIN', 'nomen_long_initial': 'LPDP', '_source_indrep': ''}
  </code></pre>