

0 / 2 embers
0 / 3000 xp
click for more info
Complete a lesson to start your streak
click for more info
Difficulty: 5
click for more info
Not enough gems
Cost: 6 gems
1: Dictionaries
incomplete
2: Duplicate Keys
incomplete
3: Accessing Dictionary Values
incomplete
4: Setting Dictionary Values
incomplete
5: Updating Dictionary Values
incomplete
6: Deleting Dictionary Values
incomplete
7: Counting Practice
incomplete
8: Iterating Over a Dictionary in Python
incomplete
9: Ordered or Unordered?
incomplete
10: Quest Status
incomplete
11: Merge Dictionaries
incomplete
Back
ctrl+,
Next
ctrl+.
This lesson's interactive features are locked, please to keep using them
We can iterate over a dictionary's keys using the same no-index syntax we used to iterate over values in a list. Use each key in square brackets to get its value.
fruit_sizes = {"apple": "small", "banana": "large", "grape": "tiny"}
for name in fruit_sizes:
size = fruit_sizes[name]
print(f"name: {name}, size: {size}")
# name: apple, size: small
# name: banana, size: large
# name: grape, size: tinyWe could have just as easily set the name variable to key or simply k.
We need to display on our players' screens what the most common enemy in a given area of the game map is.
Complete the get_most_common_enemy function. It accepts an enemies_dict dictionary that maps each enemy's name to its count:
{"jackal": 1, "kobold": 2, "soldier": 3, "gremlin": 5}When you're trying to find a "max" value, it helps to keep track of the "max so far" in a variable and to start that variable at the lowest possible number, negative infinity.
max_so_far = float("-inf")You'll also want to keep track of the enemy name associated with the maximum count. I would set the default for that variable to None.