Learn
Creating Dictionaries
Make a Dictionary
In the previous exercise we saw a dictionary that maps strings to numbers (i.e., "oatmeal": 3
). However, the keys can be numbers as well. For example, if we were mapping restaurant bill subtotals to the bill total after tip, a dictionary could look like:
subtotal_to_total = {20: 24, 10: 12, 5: 6, 15: 18}
Values can be any type. You can use a string, a number, a list, or even another dictionary as the value associated with a key!
For example:
students_in_classes = {"software design": ["Aaron", "Delila", "Samson"], "cartography": ["Christopher", "Juan", "Marco"], "philosophy": ["Frederica", "Manuel"]}
The list ["Aaron", "Delila", "Samson"]
, which is the value for the key "software design"
, represents the students in that class.
You can also mix and match key and value types. For example:
person = {"name": "Shuri", "age": 18, "siblings": ["T'Chaka", "Ramonda"]}
Instructions
1.
Create a dictionary called translations
that maps the following words in English to their definitions in Sindarin (the language of the elves):
English | Sindarin |
---|---|
mountain | orod |
bread | bass |
friend | mellon |
horse | roch |