Adding values to a Python dictionary: explanation and examples
In Python, you can add values to a dictionary in a number of ways. In this article, we'll discuss some of the most common methods.
We'll cover
- How to add a dictionary key value
- Adding multiple Python dictionary values in a key(with lists, tuples)
- how to add multiple key-value pairs at once (the
update()
method) - Take user input and add it to a dictionary
1. How to add a dictionary key value (key, value)
Unlike other data types in Python, dictionaries do not have a function for adding new values.
Therefore, to add a new key-value pair to a dictionary, use square brackets []
, as shown below.
my_dict = {}
my_dict['new_key'] = 'new_value'
print(my_dict)
# Output
{'new_key': 'new_value'}
Keys in a dictionary cannot be duplicated or modified. Therefore, lists, sets, dictionaries, and other data types that can be modified cannot be used as keys.
See this post for more details!
2. Append multiple values to value (list append()
, tuple)
Dictionaries store a single value as a value, that is, only one value. If you need to add multiple values to a single key, store a container type such as a list or tuple as the value.
In this case, use a list if you need to modify it, and a tuple if you do not.
For example,
my_dict = {}
my_dict['new_key'] = [1, 2, 3, 4]
my_dict['new_key'].append(5)
print(my_dict)
# Output
{'new_key': [1, 2, 3, 4, 5]}
Once stored, the list value can be retrieved as a key and added, deleted, and modified just like a regular list, as shown in the code above.
In our example, we used the append()
method to add a new element.
We can use tuples in the same way.
my_dict = {}
my_dict['new_key'] = (1, 2, 3, 4)
print(my_dict)
# Output
{'new_key': (1, 2, 3, 4)}
You can also store sets, dictionaries, or other container types from the collections
module as values, so use them accordingly.
3. Adding multiple key-value pairs at once
If you want to add multiple key-value pairs to a dictionary at once, you can use the update
method.
This method appends all key-value pairs from one dictionary to another like the extend() method on a list. You can use these functions to add multiple key-values.
Pass a new dictionary with the key-value pairs you want to add as an argument to the update
method, as shown in the following example.
my_dict = {}
my_dict.update({'key1': 'value1', 'key2': 'value2', 'key3': 'value값3'})
print(my_dict)
# Output
{'key1': 'value1', 'key2': 'value2', 'key3': 'value값3'}
4. Adding to the dictionary with input
The following method takes user input and adds it to the dictionary.
User input can be taken with the input()
function. This input can be stored as a key or a value as shown below.
users_age = {}
user_name = input("What is the name of user? ")
user_age = input("What is the age of user? ")
user2_name = input("What is the name of user2? ")
user2_age = input("What is the age of user2? ")
users_age[user_name] = user_age
users_age[user2_name] = user2_age
print(users_age)
# Output
What is the name of user? user1
What is the age of user? 29
What is the name of user2? user2
What is the age of user2? 25
{'user1': '29', 'user2': '25'}
Conclusion
We've seen a variety of ways to add values to Python dictionary keys.
It's a very versatile structure, and we hope you've learned to use it well.
