I have the following JSON-structure:

```
[  
  {
    "artist": "Jim",
    "nationality": "English",
    "album": "Hello!",
    "year": "2020",
    "label": "Mercury"
  },
  {
    "artist": "SoundDog",
    "nationality": "USA",
    "album": "SoundDog",
    "year": "2018",
    "label": "Private"
  },
  {
    "artist": "Jim",
    "nationality": "English",
    "album": "Wait here",
    "year": "2012",
    "label": "PrivateLabel"
  }
]

```

And I would like to get it formatted to the following structure:

```
[{
        "artist": "Jim",
        "nationality": "English",
        "albums": [{
                "album": "Hello!",
                "year": "2020",
                "label": "Mercury"
            }, {
                "album": "Wait here",
                "year": "2012",
                "label": "PrivateLabel"
            }
        ]
    }, {
        "artist": "SoundDog",
        "nationality": "USA",
        "albums": [{
                "album": "SoundDog",
                "year": "2018",
                "label": "Private"
            }
        ]
    }
]

```

I've been playing with some for-loops but am baffled how to do it in a nice and working way?

I created this short function to find albums and construct the list but I should call it only once per artist. How can I achieve that?

```
def get_albums(artist):
    albums = []
    for j in data:      # data = original JSON
        if j['artist'] == artist:
            album = {'album': j['album'], 'year': j['year'], 'label': j['label']}
            albums.append(album)
    return albums

```