
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Appending to List in Python Dictionary
Sometimes, we may need to store a list as the value of a dictionary key and in the future, for some reason we require to update or add more elements to that list. Python provides several ways for appending elements to a list in the Python dictionary. In this article, we are going to use a built-in methods 'append()', 'update()' and '+=' operator for this purpose.
Program for appending to a List in Python Dictionary
Before jumping to the program directly let's familiarize ourselves with List and Dictionary in Python.
List
It is an ordered and mutable collection of elements. It is similar to arrays of C/C++ and Java programming languages. We can create a list by placing its elements in square brackets '[ ]' separated by commas.
Instance
Fruitlist = ["apple", "banana", "orange"]
Dictionary
It is an unordered and mutable collection of elements. It stores its elements in key-value pair, where each key is associated with a value. We can create a dictionary by placing elements in curly brackets '{ }' as 'key' : 'value' and separate each key-value pair by a comma.
Instance
Details = {"Tutor" : "Tutorialspoint", "Rank" : 01, "country" : "India"}
We are going to use the following ways to append to a list in a Python dictionary:
Using append() method
This method takes an argument and add it to the end of a specified list. It has the following syntax:
Syntax
dictionary["key"].append("value")
Approach
Initialize a dictionary named 'hobbies' with three lists. It maps names to hobbies.
Use the append method to add one more element to each existing list.
Now, print the new dictionary and exit.
Example
hobbies = { "Ram": ["reading", "painting"], "Shyam": ["gaming", "coding"], "Shiv": ["cooking", "biking"] } print("The original list of hobbies: ") print(hobbies) # Appending new elements to the list in dictionary hobbies["Ram"].append("singing") hobbies["Shyam"].append("chess") hobbies["Shiv"].append("gardening") print("New list of hobbies: ") print(hobbies)
Output
The original list of hobbies: {'Ram': ['reading', 'painting'], 'Shyam': ['gaming', 'coding'], 'Shiv': ['cooking', 'biking']} New list of hobbies: {'Ram': ['reading', 'painting', 'singing'], 'Shyam': ['gaming', 'coding', 'chess'], 'Shiv': ['cooking', 'biking', 'gardening']}
Using += operator
Alternatively, we can use the '+=' operator in the place of 'append()' method to add elements to a list of the given Python dictionary. This operator is equivalent to adding the right operand to the left operand and assigning the result back to the left operand.
Example
hobbies = { "Ram": ["reading", "painting"], "Shyam": ["gaming", "coding"], "Shiv": ["cooking", "biking"] } print("The original list of hobbies: ") print(hobbies) # Appending new elements to the list in dictionary hobbies["Ram"] += ["singing"] hobbies["Shyam"] += ["chess"] hobbies["Shiv"] += ["gardening"] print("New list of hobbies: ") print(hobbies)
Output
The original list of hobbies: {'Ram': ['reading', 'painting'], 'Shyam': ['gaming', 'coding'], 'Shiv': ['cooking', 'biking']} New list of hobbies: {'Ram': ['reading', 'painting', 'singing'], 'Shyam': ['gaming', 'coding', 'chess'], 'Shiv': ['cooking', 'biking', 'gardening']} Note that we have to use the square brackets '[ ]' to wrap the element we want to append to a list. The reason behind this is the += operator expects both operands to be of the same type.
Note that we have to use the square brackets '[ ]' to wrap the element we want to append to a list. The reason behind this is the += operator expects both operands to be of the same type.
Using update() method
This method does not return any value. It simply adds a specified dictionary to another existing dictionary. It has the following syntax:
Syntax
dictionary1.update(dictionary2)
Approach
Initialize a dictionary named 'hobbies' with three keys having empty lists of values.
Now, use the update method to append list of values to the existing empty lists.
Now, print the new dictionary and exit.
Example
# creating dictionary with empty list hobbies = { "Ram": [], "Shyam": [], "Shiv": [] } print("The original list of hobbies: ") print(hobbies) # Appending new elements to the list in dictionary hobbies.update({"Ram" : ["singing"]}) hobbies.update({"Shyam" : ["chess"]}) hobbies.update({"Shiv" : ["gardening"]}) print("New list of hobbies: ") print(hobbies)
Output
The original list of hobbies: {'Ram': [], 'Shyam': [], 'Shiv': []} New list of hobbies: {'Ram': ['singing'], 'Shyam': ['chess'], 'Shiv': ['gardening']}
Conclusion
In this article, we have learned how to append new elements to an existing list in a Python dictionary. We have seen three ways of doing this operation: using the append() and update() methods and also, using the += operator. All of them modify the original dictionary in place. We also discovered the basics of lists and dictionaries in Python.