0

In order to get energy values, I am trying to iterate over this JSON result:

[{u'track_href': u'https://api.spotify.com/v1/tracks/7603o589huckPbiELnUKgu', u'analysis_url': u'https://api.spotify.com/v1/audio-analysis/7603o589huckPbiELnUKgu', u'energy': 0.526, u'liveness': 0.0966, u'tempo': 92.979, u'speechiness': 0.103, u'uri': u'spotify:track:7603o589huckPbiELnUKgu', u'acousticness': 0.176, u'instrumentalness': 0.527, u'time_signature': 4, u'danceability': 0.635, u'key': 1, u'duration_ms': 172497, u'loudness': -8.073, u'valence': 0.267, u'type': u'audio_features', u'id': u'7603o589huckPbiELnUKgu', u'mode': 1},...}]

I'm using list comprehension:

[x['energy'] for x in features]

But I'm getting the following error:

print ([x['energy'] for x in features]) 
TypeError: 'NoneType' object has no attribute '__getitem__'

What am I doing wrong here?

1
  • Can you post more code? Do you have a JSON object using json.load? Commented Aug 23, 2016 at 22:14

1 Answer 1

3

It is because within your JSON array, at some place instead of dictionary a None value is present. You may eliminate it via:

[x['energy'] for x in features if x]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.