Header Ads Widget

⚡ Premium Tools Hub • EXE Apps + Full Python Source Code
Lite • Pro • Bundle Packs • Instant Download

Python - Add Dictionary Items (Complete Guide for Beginners)

 Python dictionaries are used to store data in key-value pairs. One of the most common operations when working with dictionaries is adding new items. Python makes it easy to insert new keys, update existing values, and even add multiple items at once.

In this tutorial, you'll learn different ways to add items to a dictionary with practical examples.


What is a Dictionary?

A dictionary is a collection that stores data as key-value pairs.

Example

student = {
"name": "John",
"age": 20
}

print(student)

Output:

{'name': 'John', 'age': 20}

Adding a New Item

To add a new item, simply specify a new key and assign it a value.

Example

student = {
"name": "John",
"age": 20
}

student["grade"] = "A"

print(student)

Output:

{'name': 'John', 'age': 20, 'grade': 'A'}

Explanation

A new key called "grade" is added to the dictionary.


Adding Multiple Items

You can add several items one by one.

Example

car = {
"brand": "Toyota"
}

car["model"] = "Corolla"
car["year"] = 2025

print(car)

Output:

{'brand': 'Toyota', 'model': 'Corolla', 'year': 2025}

Using update() Method

The update() method can add new items or update existing ones.

Example

person = {
"name": "Alice",
"age": 25
}

person.update({"city": "New York"})

print(person)

Output:

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

Adding Multiple Items with update()

Example

person = {
"name": "Alice"
}

person.update({
"age": 25,
"city": "New York",
"country": "USA"
})

print(person)

Output:

{'name': 'Alice', 'age': 25, 'city': 'New York', 'country': 'USA'}

Adding Items from Another Dictionary

You can merge dictionaries using update().

Example

dict1 = {
"name": "John"
}

dict2 = {
"age": 20,
"city": "London"
}

dict1.update(dict2)

print(dict1)

Output:

{'name': 'John', 'age': 20, 'city': 'London'}

Adding Nested Dictionary Items

A dictionary can contain another dictionary.

Example

employee = {
"name": "David"
}

employee["address"] = {
"city": "Paris",
"country": "France"
}

print(employee)

Output:

{
'name': 'David',
'address': {
'city': 'Paris',
'country': 'France'
}
}

Updating an Existing Item

If the key already exists, Python updates its value.

Example

student = {
"name": "John",
"age": 20
}

student["age"] = 21

print(student)

Output:

{'name': 'John', 'age': 21}

Explanation

Since "age" already exists, its value changes from 20 to 21.


Real-World Example: Product Inventory

Example

inventory = {
"Laptop": 10,
"Mouse": 25
}

inventory["Keyboard"] = 15

print(inventory)

Output:

{
'Laptop': 10,
'Mouse': 25,
'Keyboard': 15
}

This is useful for inventory management systems.


Real-World Example: User Profile

Example

user = {
"username": "admin"
}

user["email"] = "admin@example.com"
user["role"] = "Administrator"

print(user)

Output:

{
'username': 'admin',
'email': 'admin@example.com',
'role': 'Administrator'
}

Common Mistakes

Mistake 1: Forgetting Quotes Around String Keys

❌ Wrong

student[grade] = "A"

✅ Correct

student["grade"] = "A"

Mistake 2: Expecting update() to Return a New Dictionary

❌ Wrong

new_dict = person.update({"city": "New York"})
print(new_dict)

Output:

None

✅ Correct

person.update({"city": "New York"})
print(person)

Dictionary Add Item Methods Summary

MethodDescription
dict[key] = valueAdd a single item
update()Add one or more items
update(other_dict)Merge dictionaries
Nested assignmentAdd nested dictionaries

Practice Exercise 1

Add a new key called "country" with value "Canada".

person = {
"name": "Tom",
"age": 30
}

Expected Output

{
'name': 'Tom',
'age': 30,
'country': 'Canada'
}

Practice Exercise 2

Use update() to add "salary": 5000.

employee = {
"name": "Sarah"
}

Expected Output

{
'name': 'Sarah',
'salary': 5000
}

Conclusion

Adding items to a Python dictionary is simple and efficient. You can:

  • Add a single item using square brackets.
  • Add multiple items using update().
  • Merge dictionaries together.
  • Create nested dictionaries.
  • Update existing values when needed.

Understanding how to add dictionary items is essential for working with real-world Python applications such as user profiles, inventories, databases, APIs, and configuration files.




Post a Comment

0 Comments