How To Use A Dictionary In Python

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:

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:

Step 2: Access Dictionary Values

To access a value in a dictionary, you can use the key as an index:

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:

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:

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:

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:

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:

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:

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

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.