Python Development Course
Chapter
>
Level
Using Dictionaries
Creating a dictionary
While there are many tools at your disposal for writing code more efficient, different circumstances have different best practices. Dictionaries are another way to store values, they are similar to lists but in this case hold an index that store a specific value.
Grab all the fruit in this field and use a dictionary to store them all, that is both their name and how many you managed to grab. Try it out!

Objective
Pick up all the fruit and store it in a dictionary, then count how many you’ve grabbed.
There are various fruits scatted in the forest, it be best to pick them up, these being: “Apples” , “Oranges” and “Pears” .
While variables and lists are efficient ways to store values for later use, another way to effectively store values is using dictionaries . These are similar to lists but rather than just hold a single value, they hold both a value and an index for the value.
bag = {} bag["Apples"] = Number of apples you pick up bag["Oranges"] = Number of Oranges you pick up bag["Pears"] = Number of Pears you pick up
The above code will store the name of the item in the dictionary and use it as an index (also known as a Key); in this case, the index will have a number tied to it which represents the value of how many of that item you’ve grabbed.
Once you’re done grabbing the fruits and putting them in a dictionary, count them by walking over to the X mark and using the speak() function, much like this: player.speak(bag)