

0 / 2 embers
0 / 3000 xp
click for more info
Complete a lesson to start your streak
click for more info
Difficulty: 8
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 the values in a list. With access to the dictionary's keys, we also have access to their corresponding values.
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: tiny
We 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 by iterating over all enemies in the dictionary and returning only the name of the enemy with the highest count.
If there are no enemies, return the Python None value (not a string). If there are multiple enemies with the same highest count, return the first one found.
enemies_dict is a dictionary of name -> count. Example:
{"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.