Cistoran Cistoran - 1 year ago 74
Python Question

Access Second Instance Of Key In Nested Dictionary In Python

So I'm utilizing the GMusicAPI to try to interact with Google Play Music (I'm utilizing the MobileClient view) a bit. Unfortunately it's only in Python, a language I've never used before. I'm running a search, that will search for a given artist. The problem I run into is that, some artists, the artist I want isn't the first result.

My solution around that was to just search (this search returns a dictionary) for the Top 5-10 artists and then loop through them until the name in the dictionary key matches the search query.

Here's the portion of the dictionary I'm getting back from the search. This specifically is the


[{'artist': {'name': 'Tiƫsto', 'artistArtRef': '', 'artistArtRefs': [{'kind': 'sj#imageRef', 'autogen': False, 'url': '', 'aspectRatio': '2'}], 'kind': 'sj#artist', 'artistId': 'Agzf4r7d54azste62qh6z2q7bcy', 'artist_bio_attribution': {'license_url': '', 'kind': 'sj#attribution', 'source_title': 'Wikipedia', 'license_title': 'Creative Commons Attribution CC-BY-SA 4.0', 'source_url': ''}}, 'type': '2'},

{'artist': {'name': 'Skrillex', 'artistArtRef': '', 'artistArtRefs': [{'kind': 'sj#imageRef', 'autogen': False, 'url': '', 'aspectRatio': '2'}], 'kind': 'sj#artist', 'artistId': 'Aqy2vtuiohb4rdrakrtbphxbdme', 'artist_bio_attribution': {'kind': 'sj#attribution', 'source_title': 'artist representative'}}, 'type': '2'},

{'artist': {'kind': 'sj#artist', 'artistId': 'Achiqpfv5ncaoobeiu6vfmf2jf4', 'name': 'Marshmello', 'artistArtRef': '', 'artistArtRefs': [{'kind': 'sj#imageRef', 'autogen': True, 'url': '', 'aspectRatio': '2'}, {'kind': 'sj#imageRef', 'autogen': True, 'url': '', 'aspectRatio': '1'}]}, 'type': '2'},

{'artist': {'name': 'deadmau5', 'artistArtRef': '', 'artistArtRefs': [{'kind': 'sj#imageRef', 'autogen': False, 'url': '', 'aspectRatio': '2'}], 'kind': 'sj#artist', 'artistId': 'Atngmlxlixottlthpcypidmxlu4', 'artist_bio_attribution': {'license_url': '', 'kind': 'sj#attribution', 'source_title': 'Wikipedia', 'license_title': 'Creative Commons Attribution CC-BY-SA 4.0', 'source_url': ''}}, 'type': '2'},

{'artist': {'kind': 'sj#artist', 'artistId': 'A3akm3hkaas6r4mp3nmhn2d7vom', 'name': 'Marshmello', 'artistArtRef': '', 'artistArtRefs': [{'kind': 'sj#imageRef', 'autogen': True, 'url': '', 'aspectRatio': '2'}, {'kind': 'sj#imageRef', 'autogen': True, 'url': '', 'aspectRatio': '1'}]}, 'type': '2'},

{'artist': {'kind': 'sj#artist', 'artistId': 'Aatki3zj7b6fnemq2qldpjmfg74', 'name': 'Marshmellow Overcoat', 'artistArtRef': '', 'artistArtRefs': [{'kind': 'sj#imageRef', 'autogen': True, 'url': '', 'aspectRatio': '2'}, {'kind': 'sj#imageRef', 'autogen': True, 'url': '', 'aspectRatio': '1'}]}, 'type': '2'},

{'artist': {'kind': 'sj#artist', 'artistId': 'Aik7qbnj6ml3i7fpya4rwxjswqq', 'name': 'Marshmellow', 'artistArtRef': '', 'artistArtRefs': [{'kind': 'sj#imageRef', 'autogen': True, 'url': '', 'aspectRatio': '2'}, {'kind': 'sj#imageRef', 'autogen': True, 'url': '', 'aspectRatio': '1'}]}, 'type': '2'}]

My question is how do I access anything past the first
key? I need to get the
values from it. I'm calling the first one like this.


And that works fine. However I try to access the second instance of that key like this


And it doesn't work. Any help would be greatly appreciated.

Answer Source

I think what you wish is:

#                        ---^---

because ArtistSearch['artist_hits'] is the list, you want the second dictionary [1] and inside it the ['artist']['name'] field

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download