L.Bar5522
L.Bar5522

Reputation: 147

How to iterate over nested list of dictionaries?

I need to get the 'ids' of this json response,the thing is that, there are many dictionaries with a list of dictionaries inside,how can I do this??(PS:len(items) is 20,so I need to get the 20 ids in the form of a dictionary.

{'playlists': {'href': 'https://api.spotify.com/v1/search?query=rewind-The%25&type=playlist&offset=0&limit=20',
  'items': [{'collaborative': False,
    'description': 'Remember what you listened to in 2010? Rewind and rediscover your favorites.',
    'external_urls': {'spotify': 'https://open.spotify.com/playlist/37i9dQZF1DXc6IFF23C9jj'},
    'href': 'https://api.spotify.com/v1/playlists/37i9dQZF1DXc6IFF23C9jj',
    'id': '37i9dQZF1DXc6IFF23C9jj',
    'images': [{'height': None,
      'url': 'https://i.scdn.co/image/ab67706f0000000327ba1078080355421d1a49e2',
      'width': None}],
    'name': 'Rewind - The Sound of 2010',
    'owner': {'display_name': 'Spotify',
     'external_urls': {'spotify': 'https://open.spotify.com/user/spotify'},
     'href': 'https://api.spotify.com/v1/users/spotify',
     'id': 'spotify',
     'type': 'user',
     'uri': 'spotify:user:spotify'},
    'primary_color': None,
    'public': None,
    'snapshot_id': 'MTU5NTUzMTE1OSwwMDAwMDAwMGQ0MWQ4Y2Q5OGYwMGIyMDRlOTgwMDk5OGVjZjg0Mjdl',
    'tracks': {'href': 'https://api.spotify.com/v1/playlists/37i9dQZF1DXc6IFF23C9jj/tracks',
     'total': 100},
    'type': 'playlist',
    'uri': 'spotify:playlist:37i9dQZF1DXc6IFF23C9jj'},

Im trying to get it through this:

dict={'id':''}
for playlists in playlist_data['playlists']:
    for items in playlists['items']:
        for item in items:
            for dic in range(len(item)):
                for id in dic['id']:
                    dict.update('id')
                    print(dict)

I get this error:

TypeError: string indices must be integers ```

Upvotes: 2

Views: 2635

Answers (2)

Abhishek Bhagate
Abhishek Bhagate

Reputation: 5766

You can index an object using the keys of object. I can see there are two places where id is present in an object. To retrieve those two ids and store them in a dictionary format, you can use the following approach -

_json = {
  'playlists': {
    'href': 'https://api.spotify.com/v1/search?query=rewind-The%25&type=playlist&offset=0&limit=20',
    'items': [{
      'collaborative': False,
      'description': 'Remember what you listened to in 2010? Rewind and rediscover your favorites.',
      'external_urls': {
        'spotify': 'https://open.spotify.com/playlist/37i9dQZF1DXc6IFF23C9jj'
      },
      'href': 'https://api.spotify.com/v1/playlists/37i9dQZF1DXc6IFF23C9jj',
      'id': '37i9dQZF1DXc6IFF23C9jj',
      'images': [{
        'height': None,
        'url': 'https://i.scdn.co/image/ab67706f0000000327ba1078080355421d1a49e2',
        'width': None
      }],
      'name': 'Rewind - The Sound of 2010',
      'owner': {
        'display_name': 'Spotify',
        'external_urls': {
          'spotify': 'https://open.spotify.com/user/spotify'
        },
        'href': 'https://api.spotify.com/v1/users/spotify',
        'id': 'spotify',
        'type': 'user',
        'uri': 'spotify:user:spotify'
      },
      'primary_color': None,
      'public': None,
      'snapshot_id': 'MTU5NTUzMTE1OSwwMDAwMDAwMGQ0MWQ4Y2Q5OGYwMGIyMDRlOTgwMDk5OGVjZjg0Mjdl',
      'tracks': {
        'href': 'https://api.spotify.com/v1/playlists/37i9dQZF1DXc6IFF23C9jj/tracks',
        'total': 100
      },
      'type': 'playlist',
      'uri': 'spotify:playlist:37i9dQZF1DXc6IFF23C9jj'
    }, ]
  }
}

res_dict = {'id':[items['id'], items['owner']['id']] for items in _json['playlists']['items']}
print(res_dict)

OUTPUT :

{'id': ['37i9dQZF1DXc6IFF23C9jj', 'spotify']}

If you don't need the second id that's present in the json object, you can just remove it from above res_dict and modify it as -

res_dict = {'id':items['id'] for items in _json['playlists']['items']}

This will only fetch the id present in the items array as key of any element and not any further nested ids (like items[i]->owner->id won't be in the final res as it was in the fist case ).

Upvotes: 2

Spencer Bard
Spencer Bard

Reputation: 1035

Try something like this:

ids = [item["id"] for item in json_data["playlists"]["items"]]

This is called a list comprehension.

You want to iterate over all of the "items" within the "playlists" key.

You can access that list of items:

json_data["playlists"]["items"]

Then you iterate over each item within items:

for item in json_data["playlists"]["items"]

Then you access the "id" of each item:

item["id"]

Upvotes: 4

Related Questions