top of page

Python Challenge of the Week

Reverse Dictionary Problem

# Reversing the dictionary problem

#Define the original dictionary 
artist_album = {'Beyonce': 'Homecoming', 'Taylor':'Lover', 'Carly' :'Dedicated' , 'Drake': 'Scorpion'}

 

#Initialize the reverse dictionary to an empty dictionary
album_artist = {}

 

#use a for loop to populate the reverse dictionary
for artist, album in artist_album.items():
    album_artist[album] = artist
    
print('Reversed dictionary'.center(87, '-'))
print(album_artist)
print('-'*87)
print()

​

#the side effect of using a for loop - temporary variables remain in scope
print('The side effect of a for loop - temp variables remain in scope: ', end='')

print (artist+',', album)

bottom of page