A dictionary is represented as a key-value pair. It is known as an associative array data structure where the key appears at most once in the collection.
Table of Contents
A generic example
Our company holds records of the employees. Every employee in our company has a unique identification number. The unique number is the key and the value is the name of the employee. I have added an example to visualize this scenario in Python.
mydict = { 1:"Joseph",
2: "Benjamin",
3: "Paul",
4: "Jacob"
}
print(mydict)
#output
{1: 'Joseph', 2: 'Benjamin', 3: 'Paul', 4: 'Jacob'}
We initialize a dictionary using {} curly braces. To distinguish between the key and value we can use ‘:’ a semicolon.
We understand that the keys are unique. When I tried to add another employee to key 1 it updates the value to the latest one .
mydict = { 1:"Joseph",
2: "Benjamin",
3: "Paul",
4: "Jacob",
1: "abc"
}
print(mydict)
#output
{1: 'abc', 2: 'Benjamin', 3: 'Paul', 4: 'Jacob'}
We can construct a dictionary using the built-in dict function. I have an example for you to better understand this.
mydict = dict([(1,"Joseph"),
(2, "Benjamin"),
(3, "Paul"),
(4, "Jacob")
]
)
print(mydict)
#output
{1: 'Joseph', 2: 'Benjamin', 3: 'Paul', 4: 'Jacob'}
Operations on dictionary
Accessing using Keys
We can access the keys by using the variable with [] parenthesis.
mydict[3]
#output
'Paul'
We can print all the keys in the dictionary by using keys(). If you want to access only the values then you can use values(). If you want to see all the items then use the function items().
mydict.keys()
#output
dict_keys([1, 2, 3, 4])
mydict.values()
#output
dict_values(['Joseph', 'Benjamin', 'Paul', 'Jacob'])
mydict.items()
#output
dict_items([(1, 'Joseph'), (2, 'Benjamin'), (3, 'Paul'), (4, 'Jacob')])
Iterate the map using for loop
We can iterate over the map by using one of the functions I suggested above.
for key,val in mydict.items():
print("Key: ", key, " Value:",val)
#output
Key: 1 Value: Joseph
Key: 2 Value: Benjamin
Key: 3 Value: Paul
Key: 4 Value: Jacob
Update the values in the dictionary
We can access the item using the key and update the value. I have added an example below. I just updated key 2 with a different name and we can see that change from benjamin to Steven.
mydict[2] = "Steven"
print(mydict)
#output
{1: 'Joseph', 2: 'Steven', 3: 'Paul', 4: 'Jacob'}
Deleting elements in the dictionary
You can delete an item i.e a key, value pair in the dictionary. The syntax you could follow is:
del dict[key]
I want to delete the record of Jacob. Please review the example below.
del mydict[4]
mydict
#output
{1: 'Joseph', 2: 'Steven', 3: 'Paul'}
Another example below shows that I have deleted the entire dictionary. It complains that there is nothing called mydict that can be printed out.
Other simple functions which are useful in dictionary
- Clear(): Clear the items in the dictionary.
- Example: mydict.clear(). This will remove all the keys in the dictionary
- pop(<key>): Removes the particular key in the dictionary if present.
- popitem(): Removes the last element in the dictionary. The items in the dictionary are stored in the order we add them to the dictionary. This function removes the most recently added item from the dictionary.
Conclusion
Now we know how to add, update, remove items, and many other functions. Please try these out and have some fun coding with a dictionary in Python. Click here to code and run programs yourself.
Official documentation on Dictionary is here.
Go back the the tutorial overview
Next: Coming soon