Skip to content

Instantly share code, notes, and snippets.

@gotraveltoworld
Last active February 23, 2018 06:07
Show Gist options
  • Save gotraveltoworld/f27bfd091669457ae307a8b6d759e089 to your computer and use it in GitHub Desktop.
Save gotraveltoworld/f27bfd091669457ae307a8b6d759e089 to your computer and use it in GitHub Desktop.
Python list out of index handler by iter(Facebook ads insights report)

Online ENV(repl.it): Python 3.6.1

ad = {
  'insights': {
    'outbound_clicks': [
      {
        'value': 1.0
      }
    ]
  }
}
print(next(iter(ad.get('insights', {}).get('outbound_clicks', [])), {}).get('value', 0.0))
# Result : 1.0

ad = {
  'insights': {
    'outbound_clicks': []
  }
}
print(next(iter(ad.get('insights', {}).get('outbound_clicks', [])), {}).get('value', 0.0))
# Result : 0.0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment