In this tutorial, you will learn how to use a dictionary in Python. A dictionary is a collection of key-value pairs, where each key is associated with a value.
Dictionaries are useful for storing and retrieving data quickly, and they are one of the most important data structures in Python. Some common use cases include configuration settings, translation tables, and caches.
Step 1: Create A Dictionary
To create a dictionary in Python, you can use the curly braces {}
syntax, followed by a list of key-value pairs separated by commas. Each key-value pair consists of the key and its corresponding value, separated by a colon :
. Here’s an example:
1 |
d = {"one": 1, "two": 2, "three": 3} |
The variable d
now holds a dictionary with three key-value pairs. Note that dictionary keys must be of an immutable type, such as strings, numbers, or tuples.
Alternatively, you can use the dict()
constructor to create a dictionary:
1 |
d = dict(one=1, two=2, three=3) |
Step 2: Access Dictionary Values
To access a value in a dictionary, you can use the key as an index:
1 |
value = d["one"] |
This will assign the value 1
to the variable value
. If the key does not exist in the dictionary, a KeyError
will be raised. To avoid this, you can use the get()
method, which will return a default value if the key is not found:
1 |
value = d.get("four", 4) |
In this example, since the key “four” does not exist, the value 4
will be returned.
Step 3: Modify Dictionary Values
To modify the value associated with a key, simply assign a new value to it using the key as an index:
1 |
d["one"] = 100 |
The value associated with the key “one” is now 100
. If the key does not exist, it will be added to the dictionary along with the provided value.
You can also use the update()
method to update multiple key-value pairs at once:
1 |
d.update({"one": 1, "four": 4}) |
This will update the value of “one” to 1
and add a new key-value pair “four”: 4
.
Step 4: Remove Dictionary Entries
To remove a key-value pair from a dictionary, you can use the del
keyword:
1 |
del d["one"] |
This will remove the key-value pair with the key “one” from the dictionary. If the key does not exist, a KeyError
will be raised. To avoid this, you can use the pop()
method, which will return the value associated with the key and remove it from the dictionary, or return a default value if the key is not found:
1 |
value = d.pop("one", None) |
In this example, since the key “one” does not exist, None
will be returned and the dictionary remains unchanged.
Step 5: Iterate Over Dictionary Keys and Values
To iterate over the keys and values in a dictionary, you can use the items()
method in a for loop:
1 2 |
for key, value in d.items(): print(key, value) |
This will print:
two 2 three 3 four 4
Alternatively, you can also iterate over the keys using the keys()
method or the values using the values()
method.
Full Example Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
d = {"one": 1, "two": 2, "three": 3} value = d["one"] print(value) value = d.get("four", 4) print(value) d["one"] = 100 print(d) d.update({"one": 1, "four": 4}) print(d) del d["one"] print(d) value = d.pop("one", None) print(value) for key, value in d.items(): print(key, value) |
Output
1 4 {'one': 100, 'two': 2, 'three': 3} {'one': 1, 'two': 2, 'three': 3, 'four': 4} {'two': 2, 'three': 3, 'four': 4} None two 2 three 3 four 4
Conclusion
In this tutorial, you learned how to create, access, modify, remove, and iterate over key-value pairs in a Python dictionary.
This versatile data structure is a powerful tool that you can use to solve a wide range of problems involving the efficient storage and retrieval of data.