FAQ: Creating Dictionaries - Invalid Keys

This community-built FAQ covers the “Invalid Keys” exercise from the lesson “Creating Dictionaries”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Computer Science

FAQs on the exercise Invalid Keys

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

1 Like

2 posts were split to a new topic: What does it mean that certain datatypes cannot be keys?

Can you plot your dictionary key values in matplotlib?
dict = {x: [1,2,3,4,], y:[2,4,6,6]}

Have you tried to use a search engine to see if there might be an existing answer to this question?

Is there a way to swap key:value pairs quicker than to do so manually, similar to how we were asked in the “Invalid Keys” exercise?

I have tried the following, however, the traceback that arises due to the invalid dictionary convention in the first line prevents the programme from running to the block of code that performs the swap:

children = {["Johannes", "Rosmarie", "Eleonore"]: "von Trapp", ["Sonny", "Fredo", "Michael"]: "Corleone"}

swapped_children = {value: key for key, value in children.items()}
print(swapped_children)

Thanks.

children is not a valid dictionary because:

Source: https://docs.python.org/3/tutorial/datastructures.html#dictionaries

If the keys in children were tuples of strings, then your code would work.

1 Like