title: How to extract the value of a given key based on the value of another key, from a list of nested (not-always-existing) dictionaries [Python]
body: <p>I have a <strong>list</strong> of dictionaries called <code>api_data</code>, where each dictionary has this structure:</p>
  
  <pre><code>{
  
  'location': 
      {
      'indoor': 0, 
      'exact_location': 0, 
      'latitude': '45.502', 
      'altitude': '133.9', 
      'id': 12780, 
      'country': 'IT', 
      'longitude': '9.146'
      },
  
  'sampling_rate': None, 
  'id': 91976363, 
  'sensordatavalues': 
      [   
          {
          'value_type': 'P1', 
          'value': '8.85', 
          'id': 197572463
          }, 
  
  
          {
          'value_type': 'P2', 
          'value': '3.95', 
          'id': 197572466
          }
  
          {
          'value_type': 'temperature', 
          'value': '20.80', 
          'id': 197572625
          }, 
  
          {
          'value_type': 'humidity', 
          'value': '97.70', 
          'id': 197572626
          }
      ], 
  
  'sensor': 
      {
      'id': 24645, 
      'sensor_type': 
          {
          'name': 'DHT22', 
          'id': 9, 
          'manufacturer': 
          'various'
          }, 
  
      'pin': '7'
      }, 
  
  'timestamp': '2020-04-18 18:37:50'
  
  },
  </code></pre>
  
  <p>This structure is not complete for each dictionary, meaning that sometimes a dictionary, a list element or a key is missing.</p>
  
  <p>I want to extract the value of a key when the key value of the same dictionary is equal to a certain value.<br>
  For example, for dictionary <code>sensordatavalues</code>, I want the value of the key <code>'value'</code> when <code>'value_type'</code> is equal to <code>'P1'</code>.</p>
  
  <p>I have developed this code working with for and if cycles, but I bet it is heavily inefficient. 
  How can I do it in a quicker and more efficient way?</p>
  
  <p>Please note that <code>sensordatavalues</code> always exists</p>
  
  <pre><code>for sensor in api_data:
  
      sensordatavalues = sensor['sensordatavalues']
      # L_sdv = len(sensordatavalues)
  
      for physical_quantity_recorded in sensordatavalues:
  
          if physical_quantity_recorded['value_type'] == 'P1':
  
              PM10_value = physical_quantity_recorded['value']               
  </code></pre>