I suggest a different data structure:
music = {
"Band 1": {
"Album A": ["1-Track A1", "1-Track A2", "1-Track A3"],
"Album B": ["1-Track B1", "1-Track B2", "1-Track B3"],
"Album C": ["1-Track C1", "1-Track C2", "1-Track C3"]
},
"Band 2": {
"Album A": ["2-Track A1", "2-Track A2", "2-Track A3"],
"Album B": ["2-Track B1", "2-Track B2", "2-Track B3"],
"Album C": ["2-Track C1", "2-Track C2", "2-Track C3"]
},
"Band 3": {
"Album A": ["3-Track A1", "3-Track A2", "3-Track A3"],
"Album B": ["3-Track B1", "3-Track B2", "3-Track B3"],
"Album C": ["3-Track C1", "3-Track C2", "3-Track C3"]
}
}
This is a dictionary of bands (key: band name) where each band is a dictionary containing albums (key: album name) where each album is a list containing the track names (index: track number - 1).
Then we can assume that our data structure contains only dictionaries, lists and strings. We want a function that picks a random track, i.e. a string.
Here's a recursive approach. If wanted, it could also be adapted to return the keys and indexes where it found the track as well. It's also capable of any nesting depth, so if you would want to group bands by countries or language or genre etc. that would be no problem.
import random
def pick_track(music_collection):
# we must pick a key and look that up if we get a dictionary
if isinstance(music_collection, dict):
chosen = music_collection[random.choice(list(music_collection.keys()))]
else:
chosen = random.choice(music_collection)
if isinstance(chosen, str): # it's a string, so it represents a track
return chosen
else: # it's a collection (list or dict) so we have to pick something from inside it
return pick_track(chosen)
Now we use this method like this to e.g. print 10 random tracks:
for i in range(5):
print(pick_track(music))
This could output the following example:
1-Track C1
2-Track C3
2-Track A3
3-Track A3
2-Track B1
Update:
You want to also get the keys and indexes where a track was found i.e. the band name, album name and track number? No problem, here's a modified function:
def pick_track2(music_collection):
if isinstance(music_collection, dict):
random_key = random.choice(list(music_collection.keys()))
else:
random_key = random.randrange(len(music_collection))
chosen = music_collection[random_key]
if isinstance(chosen, str):
return [random_key, chosen]
else:
return [random_key] + pick_track2(chosen)
It now does not return the track name as string, but a list of keys/indices that create the path to the picked track. You would use it like this:
for i in range(5):
print("Band: '{}' - Album: '{}' - Track {}: '{}'".format(*pick_track2(music)))
An example output:
Band: 'Band 1' - Album: 'Album C' - Track 1: '1-Track C2'
Band: 'Band 2' - Album: 'Album B' - Track 0: '2-Track B1'
Band: 'Band 1' - Album: 'Album B' - Track 0: '1-Track B1'
Band: 'Band 3' - Album: 'Album B' - Track 2: '3-Track B3'
Band: 'Band 3' - Album: 'Album B' - Track 2: '3-Track B3'
See this code running on ideone.com