How to Add Items to a Dictionary in Python?

Recently in a Python webinar the topic of discussion was on adding items to the dictionary. I explored more about this topic and In this tutorial, I will explain how to add items to a dictionary in Python along with suitable examples and screenshots.

Add Items to a Dictionary in Python

Let us learn how to add items to a dictionary in Python.

Read How to Sort a Dictionary by Value in Python?

1. Add a Single Key-Value Pair

The simplest way to add a new item to a Python dictionary is by using the square bracket notation. Here’s an example:

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

Output:

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

You can refer to the screenshot below to see the output.

Add Items to a Dictionary in Python

In this example, we start with a person dictionary containing two key-value pairs. To add a new item, we simply assign a value to a new key using the square bracket notation. Here, we add the key "city" with the value "New York" to the person dictionary. You can add a key-value pair to a dictionary using the assignment operator =.

Check out Python Sort Dictionary By Key

2. Add Multiple Key-Value Pairs

If you need to add multiple key-value pairs to a dictionary at once, you can use the update() method in Python. Here’s an example:

state_capitals = {"California": "Sacramento", "New York": "Albany"}
new_capitals = {"Texas": "Austin", "Florida": "Tallahassee"}
state_capitals.update(new_capitals)
print(state_capitals)

Output:

{'California': 'Sacramento', 'New York': 'Albany', 'Texas': 'Austin', 'Florida': 'Tallahassee'}

You can refer to the screenshot below to see the output.

How to Add Items to a Dictionary in Python

In this example, we have two dictionaries: state_capitals and new_capitals. We want to add all the key-value pairs from new_capitals to state_capitals. By calling the update() method on state_capitals and passing new_capitals as an argument, we merge the two dictionaries. The update() method is a convenient way to add multiple key-value pairs to a dictionary in Python.

3. Add Items Conditionally

Sometimes, you may want to add an item to a dictionary only if the key doesn’t already exist. You can achieve this using the Python setdefault() method. Here’s an example:

employee_roles = {"John": "Manager", "Emily": "Developer"}
employee_roles.setdefault("John", "Analyst")
employee_roles.setdefault("Michael", "Analyst")
print(employee_roles)

Output:

{'John': 'Manager', 'Emily': 'Developer', 'Michael': 'Analyst'}

You can refer to the screenshot below to see the output.

Add Items Conditionally to a Dictionary in Python

In this example, we have an employee_roles dictionary storing the roles of employees. We want to add the role “Analyst” for “John” and “Michael”, but only if they don’t already have a role assigned. By using setdefault() if the key “John” already exists, its value remains unchanged as “Manager”. However, since “Michael” doesn’t exist in the dictionary, a new key-value pair "Michael": "Analyst" is added. The setdefault() method is useful when you want to conditionally add items to a dictionary.

Read Python Increment Value in Dictionary

4. Add Items Using Loops

When you have a large number of items to add to a Python dictionary, using loops can be more efficient. Here’s an example:

cities = ["New York", "Los Angeles", "Chicago", "Houston"]
population = [8336817, 3898747, 2746388, 2320268]
city_population = {}

for city, pop in zip(cities, population):
    city_population[city] = pop

print(city_population)

Output:

{'New York': 8336817, 'Los Angeles': 3898747, 'Chicago': 2746388, 'Houston': 2320268}

In this example, we have two lists: cities and population, representing the names of cities and their respective populations. We want to create a dictionary city_population where the keys are the city names, and the values are the corresponding populations. By using a for loop and the zip() function, we iterate over the two lists simultaneously and add each city-population pair to the dictionary. Loops provide a concise way to add multiple items to a dictionary based on existing data.

Check out How to Find Duplicate Values in Dictionary Python

Conclusion

In this tutorial, I explained how to add items to a dictionary in Python. We learned how to add a single key-value pair using square bracket notation, add multiple pairs using the update() method, conditionally add items with setdefault(), and efficiently add items using loops.

You can also read:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.