How to add key-value to a dictionary in Python?

Python comes with a built-in data type called a dictionary that allows you to store and manipulate key-value pairs. Adding key-value pairs to a dictionary is a common task in Python programming. In this article, we will explore different ways to add key-value pairs to a dictionary in Python.

Using the assignment operator (=)

One of the simplest and most common ways to add a key-value pair to a dictionary in Python is by using the assignment operator (=). Here’s an example:


my_dict = {'name': 'John', 'age': 25}
my_dict['city'] = 'New York'
print(my_dict)

Output:

{'name': 'John', 'age': 25, 'city': 'New York'}

You can add a key-value pair to a dictionary by assigning a value to a new or existing key using the assignment operator (=).

Using the update() method

Another way to add key-value pairs to a dictionary is by using the update() method. This method allows you to merge a dictionary with another dictionary or with an iterable of key-value pairs. Here’s an example:


my_dict = {'name': 'John', 'age': 25}
my_dict.update({'city': 'New York'})
print(my_dict)

Output:

{'name': 'John', 'age': 25, 'city': 'New York'}

You can use the update() method to add key-value pairs to a dictionary by passing a dictionary or an iterable of key-value pairs as an argument.

Using the setdefault() method

The setdefault() method provides a simple way to add a key-value pair to a dictionary if the key does not already exist. If the key already exists, it returns the value associated with the key. Here’s an example:


my_dict = {'name': 'John', 'age': 25}
city = my_dict.setdefault('city', 'New York')
print(my_dict)
print(city)

Output:

{'name': 'John', 'age': 25, 'city': 'New York'}
New York

The setdefault() method allows you to add a key-value pair to a dictionary only if the key does not already exist.

Using defaultdict

The defaultdict class from the collections module provides an easy way to add key-value pairs to a dictionary in Python. It automatically adds a default value to the dictionary if the key does not exist. Here’s an example:


from collections import defaultdict

my_dict = defaultdict(int)
my_dict['apple'] = 5
my_dict['banana'] = 2
print(my_dict)

Output:

{'apple': 5, 'banana': 2}

The defaultdict class automatically adds a default value to the dictionary if the key does not exist.

FAQs:

How do you add multiple key-value pairs to a dictionary at once?

You can add multiple key-value pairs to a dictionary at once by using the update() method with another dictionary or an iterable of key-value pairs as an argument.

Can I add a key-value pair to a dictionary using a variable?

Yes, you can use a variable to add a key-value pair to a dictionary by assigning the value of the variable to a new or existing key.

What happens if I add a key-value pair to a dictionary using an existing key?

If you add a key-value pair to a dictionary using an existing key, the value associated with that key will be updated.

How do I check if a key exists in a dictionary before adding a key-value pair?

You can use the ‘in’ keyword to check if a key exists in a dictionary before adding a key-value pair.

Can I add a key-value pair to a dictionary in a specific position?

No, dictionaries in Python are unordered, so you cannot add a key-value pair to a specific position.

Dive into the world of luxury with this video!


Your friends have asked us these questions - Check out the answers!

Leave a Comment