In this tutorial, we will take a deep dive into the world of Python and specifically learn about Dictionaries – one of the most powerful data structures in Python. Why are they useful? They are mutable, can hold a variety of data types, and perhaps most importantly- come with built-in functions that make manipulations and operations extremely easy.
What is a Dictionary?
In Python, a Dictionary is a collection of key-value pairs where the key must be unique. Dictionaries are unordered collections and the values in a dictionary can be of any type while the keys can be of any hashable type.
Creating a Dictionary
Creating a dictionary in Python is uncomplicated. You just need to put the data inside the curly braces {} separating each key-value pair by a comma (,). The key and value are separated using a colon(:).
1 |
dict1 = {"key1": "value1", "key2": "value2", "key3": "value3"} |
Accessing Values in a Dictionary
Accessing the values of a dictionary is straightforward. You can simply use the square bracket notation with the key, like dict[“key”].
1 |
value = dict1["key1"] |
You can also use the get() method which is a safer way to access a value because it returns None instead of an error if the key does not exist.
1 |
value = dict1.get("key1") |
Modifying a Dictionary
Dictionaries are mutable data structures, meaning you can change their values. To modify a dictionary, you can simply assign a new value to a key.
1 |
dict1["key1"] = "new value" |
Adding and Removing Items
Adding a new key-value pair in a dictionary is as easy as defining a new value for that key.
1 |
dict1["key4"] = "value4" |
To remove a key-value pair, you can use the del keyword or the dictionary’s pop() method. The pop() method removes the item with the specified key name:
1 |
dict1.pop("key4") |
Looping Through a Dictionary
You can traverse through Python dictionaries using loops. Here are three ways in which you can do dictionary traversal in Python:
- Loop through Keys:
1for key in dict1: print(key)
- Loop through Values:
1for value in dict1.values(): print(value)
- Loop through Key-Value pairs:
1for key, value in dict1.items(): print(key, value)
Full Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
dict1 = {"key1": "value1", "key2": "value2", "key3": "value3"} print("Access:", dict1["key1"]) dict1["key1"] = "new value" print("Modified:", dict1) dict1["key4"] = "value4" print("Added:", dict1) dict1.pop("key4") print("Removed:", dict1) for key in dict1: print("Key:", key) for value in dict1.values(): print("Value:", value) for key, value in dict1.items(): print("Key, Value:", key, value) |
Conclusion
Dictionaries in Python are a crucial data structure that should be in any Python programmer’s toolbox. One can store data, navigate through it, modify, and remove it with relative ease which makes dictionaries extremely useful in handling data in Python.
The examples and code snippets provided in this tutorial should serve as a good jumping-off point in your exploration of Python dictionaries.