Dictating A Dictionay: Snake Count His Toys
How Mapping Helps ?

Recall this article where we discussed why the snake put his toys back in the toy box. Imagine our snake has many of the same toys. For example, just imagine he has three airplanes, five remote control cars, and ten stuffed toys. Our snake put all his toys in the toy box randomly and in a hurry (because his mom was angry) without counting them. After a few days, he picked up his toy box and began to look for his remote control Mercedes toy car, but he couldn’t find it. He somehow forgot to put his car inside the toy box. To confirm his mistake, he began counting his toys. Let’s see how he does that and how this counting helps him confirm his suspicion of not putting his toy car back in the box.
‘Mapping’ Each Toy With Its Quantity
Here, by mapping I mean connecting one value to another. How is our snake going to count the toys so that he can remember the quantity of each category of toy? Our snake’s goal is to map a category of toy with the quantity of that toy present in the box.
So he starts mapping each category of toy one by one. For example, if he has ten stuffed toys, then he will make a category on paper named stuffed toys, and in front of it, he writes the total number of stuffed toys present inside the box.

By mapping, it is less confusing for the snake to mix up the quantity of toys with the toy's category. For example, if he hadn't done the mapping, it is very possible that he would get confused between 10 stuffed toys and 12 stuffed toys.

After storing all the quantities of each category of toy, our snake knows exactly how many remote control cars he has. He asks his mom about the car and tells her that he has twelve remote control cars and one is missing. His mom already knows how many cars he really has (you know mothers know everything about their kids). She tells him twelve is the right number and he must have dreamed about a thirteenth toy car. And guess what, mom is right; our snake had a dream that he had an extra car with him.
Now you see how mapping toys helped our snake to know the truth. The story is over; let's get technical.
Dictionary And Mapping (Structure Of A Dictionary)
Mapping is connecting one value to another, but how does a dictionary in Python map the values we give? The answer lies in the structure of a dictionary in Python.
A dictionary in Python has key:value pairs stored inside it. We can understand it as ‘Unique Key‘ connects to ‘Its Value‘ (‘:’ simply connects or maps our key to its value). Remember, the key must be a unique and immutable data object. (To know what immutable objects are, click here).
From our story above, we can assume that ‘Toy Type‘ is the key and ‘Number of each type of toy‘ is the value. So what our snake is doing is assigning a value to a unique key. Remember, Toy Type must be unique; if not, then the value gets updated. Let’s see how...

To understand why key must be unique see the below image…

Because of this value mapping concept, dictionary also known as Hash Map or Hash Table in other languages like c++, java, etc.
Dictionaries are mutable meaning we can update the values or insert new values in our dictionaries(It is similar as if our snake get more toys and he keep updating his counting via mapping)
Defining Dictionaries
There are few main methods using which we can initialize our dictionary in Python.
Using
{}To initailize dictionay in Python you just assign
{}to some varaible. Python will automatically put that variable as an object of dictionary class. Remeber {} are also used for sets but in dictionaries we enter value inside {} as key:value pair. See below for clarity…
Using
dict()As all the things in Python, dictionary also is a class in python to intialize an object of dictionary class we can call
dict()constructor.dict()helps to intialize an object of dictionary class and hence, help us to create dictionary in Python. See below for clarity…
Operation On Dictionaries
Insertion
There are two ways in which we can insert key:value elements inside our dictionry. Let’s see both..
Normal Insertion
In this type of insertion we normally assign value to a unique key.
The syntax is
dictionary_name[Key] = Value. Key must be immutable data object. Value can be mutable or immutable . For more clarity see the image..
Notice in below image, our dictionary is giving error if we define our key as mutable data type.

But, if key is immutable and value is mutable then, it is fine. Python will give zero errors in that case.

Comprehensive Insertion
In this type of insertion, we can insert multiple key:value pairs inside our dictionary using inline loop inside a dictionary. Using inline loop inside a dictionary is known as dictionary comprehension (List also have list comprehension). See the image for clarity.

Deletion
In dictionaries we can delete a Key:Value pair using keys only. We cannot delete pairs in dictionary by values. There are several methods to delete those key:value pairs, let’s discuss them.
Using ‘del’
If we want to delete a specific key:value pair, we can do that using del keyword. Let’s see how.
snake_toys = {'Stuff Toys': 10, 'Remote Control Cars': 12, 'Normal Cars': 4, 'Blocks': 20} del snake_toys['Normal Cars'] print(snake_toys) #Output -> {'Stuff Toys': 10, 'Remote Control Cars': 12, 'Blocks': 20} (Noraml car key and its value got deleted)Remember, if we try to delete a key which is not present inside our dictionary then it will give Key error. See the image below.

Using ‘pop()’
If we want to delete a specific key in our dictionary and want to return the value of deleted key, then we use pop() method. It takes one argument which is the key we want to delete and return its value.

Using ‘popitem()’
popitem() method used to delete the last key:value pair you insert inside your dictionary. It return the deleted key:value pair which we can print. See the image below..

Using ‘clear()’
To delete all the key:value pairs inside our dictionary and make it empty, we can use clear() method.

Traversal
We can traverse dictionary using its keys, values or both. Let’s see how
Using Keys
We can traverse through our dictionary using for loop and key() method on that dictionay.

Using Values
We can traverse through our dictionary using for loop and value() method. See below for clarity..

Using Keys and Values
We can get both keys and values at the same time by using item() method on our dictionary.

Searching
We can search for a particular key or value inside our dictionary simply by using the in operator. If the key or value is present, it will return True, or we can print that key or value if we want to. In searching, we use traversing with the if condition to determine whether the item we need really exists or not.
See the image for clarity..

Accessing
We can access values in our dictionary using several ways. Let’s discuss them..
Using get()
get()method is used to get the value of a particular key present in the dictionary. If the key is not present then it will return None. get() method take one argument which is the key whose value you want to access from dictionary.
Direct Access
We can directly access the value of a key present in our dictionary using syntax
dictionary_name[Key]. It will return the value of the key you write inside those square brackets, we can then print our value.
It will raise Keyerror if key doesn’t exist. See image for clarity.

Using Loops
We can access each key, value or both at the same time using loops just like we do in dictionary traversal.

Updating
As we know, a dictionary is a mutable object, which means we can change, update, or add new key:value pairs in our dictionary. Let’s see how we do that.

Assigning New Value
We can access the value of a particular key in a dictionary using the
dictionary_name[key]syntax. Once we get the previous value, we can then change that value to the new value. See the image for more clarity.
Using update()
To update or add multiple key:value pairs inside our previously defined dictionary, we can use the update() method. It takes another dictionary as an argument, update({}), in which we define the key:value pairs we want to update or add. See the image for clarity.

Renaming a Key
We can rename a key using pop() method. See the image for clarity.

Summary
This article introduces the concept of mapping through a playful story of a snake organizing his toys, paralleling it with Python dictionaries. It explains how dictionaries use key-value pairs for mapping, ensuring that keys are unique and immutable. The article dives into dictionary operations such as insertion, deletion, traversal, searching, accessing, and updating, offering examples and explanations for each. It also highlights the use of dictionary methods like `del`, `pop()`, `popitem()`, `clear()`, `get()`, and `update()`, providing a comprehensive guide to managing data with dictionaries in Python.
Thanks for reading. Don’t forget to share it with other learners..
For more articles click here
#ChaiCode



