Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Python Collections (2016, retired 2019) Dictionaries Dictionary Basics

How do I associate a list to the key?

Hi guys, how do I associate the list[1, 2, 3, 4] to the key "levels" inside dictionary?

dicts.py
player = {"name": "string", "remaining_lives": 3}

dictionary = {["levels", [1,2,3,4]], "items": {"key1": 3, "key2": "value2"}}

1 Answer

Chris Jones
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Chris Jones
Java Web Development Techdegree Graduate 23,933 Points

Hey Tiago,

Good job creating the player dictionary! That's a good start!

In order to add another key and value to the player dictionary, I would recommend first creating a new list variable:

numbers_list = [1,2,3,4]

Then add the numbers_list variable to the player dictionary:

player["levels"] = numbers_list

Try the same thing with when you add the key items to the player dictionary. Try creating a new dictionary variable (first code snippet above), then add the new dictionary variable to the player dictionary (second snippet above).

Let me know if you have any questions on this part of the code challenge or the next part. Keep it up :)!

Hey Chris thanks for the tip, but for this to work the dictionary must be empty right? I did this, but still isn't working:

dictionary = { }

numbers_list = [1,2,3,4] dictionary["levels"] = numbers_list

items_dict = {"key1": 3, "key2": "value2"} dictionary["items"] = items_dict

Chris Jones
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Chris Jones
Java Web Development Techdegree Graduate 23,933 Points

Hey Taigo,

No, you don't have to have an empty dictionary.

The code challenge asks for a player dictionary, but I don't see a player dictionary in your answer.

In your original question you already had this:

player = {"name": "string", "remaining_lives": 3}

Now, we need to add a list to the player dictionary, which it looks like you did, but just didn't add it to the player dictionary - instead you added it to a new dictionary. You want to do this instead:

number_list = [1,2,3,4]
player["levels"] = number_list

Lastly, you want to do the same thing, but with a new dictionary object. Try this:

item_dict = {"weapon":"sword"}
player["items"] = item_dict

Let me know if you have any more questions!