Question: Create the function swap that takes a dictionary and returns a dictionary where the keys and values have been swapped. If the original dictionary contains
Create the function swap that takes a dictionary and returns a dictionary where the keys and values have been swapped. If the original dictionary contains an unhashable value, return the string:
Cannot swap the keys and values for this dictionary
Test Case
Using the given dictionary and function call below:
# test code below
if namemain:
exampledict
: 'one',
: 'two',
: 'three'
swapped swapexampledict
printswapped
Your script should print:
one: 'two': 'three':
TRY IT
Test Case
Using the given dictionary and function call below:
# test code below
if namemain:
exampledict
:
: 'four',
: 'five'
swapped swapexampledict
printswapped
Your script should print:
Cannot swap the keys and values for this dictionary
TRY IT
Test Case
Using the given dictionary and function call below:
# test code below
if namemain:
exampledict
: 'one',
: :
: 'five'
swapped swapexampledict
printswapped
Your script should print:
Cannot swap the keys and values for this dictionary
TRY IT
Test Case
Using the given dictionary and function call below:
# test code below
if namemain:
exampledict
: 'one',
: 'two',
:
swapped swapexampledict
printswapped
Your script should print:
one: 'two': :
TRY IT
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
