FAQ: Creating Dictionaries - Invalid Keys


#1

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!


#2

We can have a list or a dictionary as a value of an item in a dictionary, but we cannot use these data types as keys of the dictionary. If we try to, we will get a TypeError . i really dont understand these lines


#3

dictionaries consist of key value pairs:

d = {
   'key': 'value'
}

what these lines are saying is that the value of a dictionary can be a dictionary or a list:

d = {
   'my_list': ['item1', 'item2', 'item3'],
   'my_dict': {'key': 'value'}
}

the keys (my_list, my_dict and key) are strings. So that is valid. The moment you attempt to use a list or dictionary as key for a dictionary, you will get a type error.