Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Appending to list in Python Dictionary
Sometimes, we may need to store a list as the value of a dictionary key and later update or add more elements to that list. Python provides several ways for appending elements to a list in a Python dictionary. In this article, we'll explore the append() method, += operator, and update() method for this purpose.
Understanding Lists and Dictionaries
Before diving into the methods, let's understand the basic data structures we're working with.
List
A list is an ordered and mutable collection of elements. We create lists by placing elements in square brackets separated by commas ?
fruits = ["apple", "banana", "orange"] print(fruits)
['apple', 'banana', 'orange']
Dictionary
A dictionary is an unordered and mutable collection that stores elements in key-value pairs. We create dictionaries using curly brackets ?
details = {"Tutor": "Tutorialspoint", "Rank": 1, "country": "India"}
print(details)
{'Tutor': 'Tutorialspoint', 'Rank': 1, 'country': 'India'}
Using append() Method
The append() method adds a single element to the end of an existing list. When the dictionary value is a list, we can directly call append() on it ?
Syntax
dictionary["key"].append("value")
Example
hobbies = {
"Ram": ["reading", "painting"],
"Shyam": ["gaming", "coding"],
"Shiv": ["cooking", "biking"]
}
print("Original hobbies:")
print(hobbies)
# Appending new elements to existing lists
hobbies["Ram"].append("singing")
hobbies["Shyam"].append("chess")
hobbies["Shiv"].append("gardening")
print("\nUpdated hobbies:")
print(hobbies)
Original hobbies:
{'Ram': ['reading', 'painting'], 'Shyam': ['gaming', 'coding'], 'Shiv': ['cooking', 'biking']}
Updated hobbies:
{'Ram': ['reading', 'painting', 'singing'], 'Shyam': ['gaming', 'coding', 'chess'], 'Shiv': ['cooking', 'biking', 'gardening']}
Using += Operator
The += operator extends the list by adding elements from another iterable. Note that we must wrap single elements in square brackets to create a list ?
Example
hobbies = {
"Ram": ["reading", "painting"],
"Shyam": ["gaming", "coding"],
"Shiv": ["cooking", "biking"]
}
print("Original hobbies:")
print(hobbies)
# Using += operator to add elements
hobbies["Ram"] += ["singing"]
hobbies["Shyam"] += ["chess"]
hobbies["Shiv"] += ["gardening"]
print("\nUpdated hobbies:")
print(hobbies)
Original hobbies:
{'Ram': ['reading', 'painting'], 'Shyam': ['gaming', 'coding'], 'Shiv': ['cooking', 'biking']}
Updated hobbies:
{'Ram': ['reading', 'painting', 'singing'], 'Shyam': ['gaming', 'coding', 'chess'], 'Shiv': ['cooking', 'biking', 'gardening']}
Note: We must use square brackets around the element because the += operator expects both operands to be of the same type (both lists).
Using update() Method
The update() method merges another dictionary into the existing one. This replaces entire values, so it's useful when you want to replace the existing list entirely ?
Syntax
dictionary1.update(dictionary2)
Example
# Creating dictionary with empty lists
hobbies = {
"Ram": [],
"Shyam": [],
"Shiv": []
}
print("Original hobbies:")
print(hobbies)
# Using update() to add lists
hobbies.update({"Ram": ["singing"]})
hobbies.update({"Shyam": ["chess"]})
hobbies.update({"Shiv": ["gardening"]})
print("\nUpdated hobbies:")
print(hobbies)
Original hobbies:
{'Ram': [], 'Shyam': [], 'Shiv': []}
Updated hobbies:
{'Ram': ['singing'], 'Shyam': ['chess'], 'Shiv': ['gardening']}
Comparison of Methods
| Method | Purpose | Best For |
|---|---|---|
append() |
Adds single element | Adding one item at a time |
+= |
Extends with multiple elements | Adding multiple items |
update() |
Replaces entire value | Replacing existing lists |
Conclusion
Use append() to add single elements, += to extend lists with multiple elements, and update() to replace entire list values. All methods modify the original dictionary in place, making them efficient for list manipulation within dictionaries.
