Adding Key and Value to Dictionary in Python
Python dictionaries are a fundamental data structure in the programming language. They are mutable, meaning they can be modified after creation. One of the key features of dictionaries is that they can store key-value pairs, where each key is unique and maps to a specific value. In this article, we will explore how to add key and value to a dictionary in Python.
Creating a Dictionary
Before we can add key and value to a dictionary, we need to create one. We can create a dictionary using the dict() function or by assigning key-value pairs to a dictionary literal.
Creating a Dictionary with Key-Value Pairs
Here’s an example of creating a dictionary with key-value pairs:
# Create a dictionary with key-value pairs
person = {
"name": "John Doe",
"age": 30,
"city": "New York"
}
print(person)
Output:
{'name': 'John Doe', 'age': 30, 'city': 'New York'}
Creating a Dictionary with Key-Value Pairs Using a Dictionary Literal
We can also create a dictionary using a dictionary literal:
# Create a dictionary with key-value pairs using a dictionary literal
person = {"name": "John Doe", "age": 30, "city": "New York"}
print(person)
Output:
{'name': 'John Doe', 'age': 30, 'city': 'New York'}
Adding Key and Value to a Dictionary
Once we have created a dictionary, we can add key and value to it using the following methods:
Adding Key and Value to a Dictionary
Here’s an example of adding key and value to a dictionary:
# Create a dictionary
person = {
"name": "John Doe",
"age": 30,
"city": "New York"
}
# Add a new key-value pair
person["country"] = "USA"
# Print the updated dictionary
print(person)
Output:
{'name': 'John Doe', 'age': 30, 'city': 'New York', 'country': 'USA'}
Adding Multiple Key-Value Pairs to a Dictionary
We can also add multiple key-value pairs to a dictionary:
# Create a dictionary
person = {
"name": "John Doe",
"age": 30,
"city": "New York"
}
# Add multiple key-value pairs
person["country"] = "USA"
person["hobbies"] = ["swimming", "reading"]
# Print the updated dictionary
print(person)
Output:
{'name': 'John Doe', 'age': 30, 'city': 'New York', 'country': 'USA', 'hobbies': ['swimming', 'reading']}
Accessing Key and Value from a Dictionary
Once we have added key and value to a dictionary, we can access them using the following methods:
Accessing Key and Value from a Dictionary
Here’s an example of accessing key and value from a dictionary:
# Create a dictionary
person = {
"name": "John Doe",
"age": 30,
"city": "New York"
}
# Access the value of a key
print(person["name"]) # Output: John Doe
# Access the value of a key using its index
print(person[0]) # Output: John Doe
# Access the value of a key using its key
print(person["name"]) # Output: John Doe
Updating Key and Value from a Dictionary
We can also update key and value from a dictionary:
# Create a dictionary
person = {
"name": "John Doe",
"age": 30,
"city": "New York"
}
# Update the value of a key
person["age"] = 31
# Print the updated dictionary
print(person)
Output:
{'name': 'John Doe', 'age': 31, 'city': 'New York'}
Removing Key and Value from a Dictionary
We can also remove key and value from a dictionary:
Removing Key and Value from a Dictionary
Here’s an example of removing key and value from a dictionary:
# Create a dictionary
person = {
"name": "John Doe",
"age": 30,
"city": "New York"
}
# Remove the key-value pair
del person["age"]
# Print the updated dictionary
print(person)
Output:
{'name': 'John Doe', 'city': 'New York'}
Removing Multiple Key-Value Pairs from a Dictionary
We can also remove multiple key-value pairs from a dictionary:
# Create a dictionary
person = {
"name": "John Doe",
"age": 30,
"city": "New York"
}
# Remove multiple key-value pairs
del person["age"]
del person["city"]
# Print the updated dictionary
print(person)
Output:
{'name': 'John Doe'}
Common Dictionary Methods
Here are some common dictionary methods:
keys(): Returns a view object that displays a list of all keys in the dictionary.values(): Returns a view object that displays a list of all values in the dictionary.items(): Returns a view object that displays a list of all key-value pairs in the dictionary.get(): Returns the value for a given key if it exists in the dictionary. If it doesn’t exist, it returnsNone.update(): Updates the dictionary with the key-value pairs from another dictionary.pop(): Removes and returns the value for a given key. If the key doesn’t exist, it returnsNone.popitem(): Removes and returns the first key-value pair from the dictionary.clear(): Removes all key-value pairs from the dictionary.
Conclusion
In this article, we have explored how to add key and value to a dictionary in Python. We have covered the basics of creating a dictionary, adding key and value, accessing key and value, updating key and value, removing key and value, and common dictionary methods. We have also provided examples and code snippets to help illustrate these concepts. By following these guidelines, you can effectively use dictionaries in your Python programs to store and manipulate data.
