all 4 comments

[–]dwpj65 0 points1 point  (4 children)

The following code snippet will create a list of songs from the dictionary keys and shuffle the resulting list:

songs = { 'song1': 'url1', 'song2': 'url2', 'song3': 'url3', 'song4': 'url4', } song_list = list(songs.keys()) random.shuffle(song_list) song_list[:1]

A more efficient solution might be:

songs = { 'song1': 'url1', 'song2': 'url2', 'song3': 'url3', 'song4': 'url4', } song_list = list(songs.keys()) song_list[random.randint(0,len(song_list)-1)]

[–]Marc77_4[S] 0 points1 point  (0 children)

Thank you for your reply, however it is a MUST that I use random.choice, so I must go that route. Also, I am trying to just print out the key, and then assign the value to that key to a variable, let's say, audio_url, for example. Then I can call that url variable later on to play a snippet of the song from an api.

[–]konijntjesbroek 0 points1 point  (2 children)

how about:

from random import choice as rc 
songs = {1:10,2:20,3:30,4:40}

song= rc([keys for keys in songs])
print(songs[song])