In this tutorial, you will learn how to fill an empty dictionary in Python. Dictionaries are an important data structure in Python that allows you to store key-value pairs. Sometimes, you might start with an empty dictionary and fill it with data later on in your program. Let’s take a look at different ways to fill an empty dictionary in Python.
Step 1: Create an Empty Dictionary
First, you need to create an empty dictionary. You can do this using two methods: either by using the {}
syntax or by calling the dict()
constructor.
1 2 3 4 5 |
# Using curly braces empty_dict1 = {} # Using the dict() constructor empty_dict2 = dict() |
Step 2: Add Items to the Dictionary Using the Square Bracket Syntax
To add items to a dictionary, use the square bracket []
syntax with the key and its corresponding value.
1 2 3 4 5 6 7 |
empty_dict1["key1"] = "value1" empty_dict1["key2"] = "value2" empty_dict1["key3"] = "value3" print(empty_dict1) # Output: # {'key1': 'value1', 'key2': 'value2', 'key3': 'value3'} |
Step 3: Add Items Using the .update() Method
You can also fill an empty dictionary using the update()
method. The update()
method adds items to a dictionary from another dictionary, iterable, or keyword arguments. If the key already exists in the dictionary, the value will be updated; if the key does not exist, the key-value pair will be added to the dictionary.
1 2 3 4 5 6 |
empty_dict2.update({"key4": "value4", "key5": "value5"}) empty_dict2.update(key6="value6") print(empty_dict2) # Output: # {'key4': 'value4', 'key5': 'value5', 'key6': 'value6'} |
Step 4: Using a Dictionary Comprehension
Dictionary comprehensions are a concise way to create or fill dictionaries. The following example demonstrates how to fill a dictionary using a dictionary comprehension that generates key-value pairs.
1 2 3 4 5 |
empty_dict3 = {f"key{i}": f"value{i}" for i in range(1, 4)} print(empty_dict3) # Output: # {'key1': 'value1', 'key2': 'value2', 'key3': 'value3'} |
Full Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
# Create empty dictionaries empty_dict1 = {} empty_dict2 = dict() # Add items to empty_dict1 using square bracket syntax empty_dict1["key1"] = "value1" empty_dict1["key2"] = "value2" empty_dict1["key3"] = "value3" print(empty_dict1) # Add items to empty_dict2 using .update() method empty_dict2.update({"key4": "value4", "key5": "value5"}) empty_dict2.update(key6="value6") print(empty_dict2) # Using a dictionary comprehension empty_dict3 = {f"key{i}": f"value{i}" for i in range(1, 4)} print(empty_dict3) |
Output:
{'key1': 'value1', 'key2': 'value2', 'key3': 'value3'} {'key4': 'value4', 'key5': 'value5', 'key6': 'value6'} {'key1': 'value1', 'key2': 'value2', 'key3': 'value3'}
Conclusion
In this tutorial, you learned how to fill an empty dictionary in Python using different methods, such as the square bracket syntax, the update()
method, and dictionary comprehensions. Now you should have a better understanding of how to work with dictionaries in Python and how they help you to store and manipulate key-value pairs.