6.1k views
2 votes
12.2 question 3 please help

Instructions

Write a method swap_values that has three parameters: dcn, key1, and key2. The method should take the value in the dictionary dcn stored with a key of key1 and swap it with the value stored with a key of key2. For example, the following call to the method
positions = {"C": "Anja", "PF": "Jiang", "SF": "Micah", "PG": "Devi", "SG": "Maria"}
swap_values(positions, "C", "PF")
should change the dictionary positions so it is now the following:
{'C': 'Jiang', 'PF': 'Anja', 'SF': 'Micah', 'PG': 'Devi', 'SG': 'Maria'}



User Vks
by
7.6k points

1 Answer

4 votes

Answer:

def swap_values(dcn, key1, key2):

temp = dcn[key1] # store the value of key1 temporarily

dcn[key1] = dcn[key2] # set the value of key1 to the value of key2

dcn[key2] = temp # set the value of key2 to the temporary value

positions = {"C": "Anja", "PF": "Jiang", "SF": "Micah", "PG": "Devi", "SG": "Maria"}

print("Initial dictionary: ")

print(positions)

swap_values(positions, "C", "PF")

print("Modified dictionary: ")

print(positions)

Step-by-step explanation:

User Antoine Grenard
by
7.6k points