Python dictionaries are powerful data structures that allow you to store and retrieve data using key-value pairs. Appending an item to a list within a dictionary involves accessing the list by its corresponding key and then using the append() method to add the new item to the list.
Example
Input: {'Destination': 'China', 'Nationality': 'Italian', 'Age': [20]} Output: {'Destination': 'China', 'Nationality': 'Italian', 'Age': [20, 'Twenty']} Explaination: Appended "Twenty" in the list of keys of Age.
Appending to List in Python Dictionary
In Python, we can append to a dictionary in several ways:
- Using += Operator
- Using List append() Method
- Using List append() Method
- Using defaultdict() Method
- Using update() Function
- Appending Values to a List in Key
Using += Operator
In this method, we will use the += operator to append a Python list into the dictionary, for this we will take a dictionary and then add elements as a list into the dictionary.
Python3
Details = { "Destination" : "China" , "Nationality" : "Italian" , "Age" : []} print ( "Original:" , Details) # appending the list Details[ "Age" ] + = [ 20 , "Twenty" ] print ( "Modified:" , Details) |
Output:
Original: {'Destination': 'China', 'Nationality': 'Italian', 'Age': []}
Modified: {'Destination': 'China', 'Nationality': 'Italian', 'Age': [20, 'Twenty']}
Using List append() Method
In this method, we will use conditions for checking the key and then append to a dictionary list using the list append() method.
Python3
Details = { "Destination" : "China" , "Nationality" : "Italian" , "Age" : [ 20 ]} print ( "Original:" , Details) if "Age" in Details: Details[ "Age" ].append( "Twenty" ) print ( "Modified:" , Details) |
Output:
Original: {'Destination': 'China', 'Nationality': 'Italian', 'Age': [20]}
Modified: {'Destination': 'China', 'Nationality': 'Italian', 'Age': [20, 'Twenty']}
Using defaultdict() Method
In this method, we are using the defaultdict() function. It is a part of the collections module. We have to import the function from the collections module to use it in the program and then use it to append to a dictionary list. Since append takes only one parameter, to insert another parameter, repeat the append method.
Python3
from collections import defaultdict Details = defaultdict( list ) print ( "Original:" , Details) Details[ "Country" ].append( "India" ) Details[ "Country" ].append( "Pakistan" ) print ( "Modified:" , Details) |
Output:
Original: defaultdict(<class 'list'>, {})
Modified: defaultdict(<class 'list'>, {'Country': ['India', 'Pakistan']})
Using update() Function
We will use the Python dictionary update() function to add a new list to the dictionary. We can use the update() function to embed a dictionary inside another dictionary.
Python3
Details = { "Destination" : "China" , "Nationality" : "Italian" } Details[ "Age" ] = [] print ( "Original:" , Details) # using update() function Details.update({ "Age" : [ 18 , 20 , 25 , 29 , 30 ]}) print ( "Modified:" , Details) |
Output:
Original: {'Destination': 'China', 'Nationality': 'Italian', 'Age': []}
Modified: {'Destination': 'China', 'Nationality': 'Italian', 'Age': [18, 20, 25, 29, 30]}
Appending Values to a List in Key
You can convert a list into a value for a key in a Python dictionary using dict() function.
Python3
Values = [ 18 , 20 , 25 , 29 , 30 ] Details = dict ({ "Age" : Values}) print (Details) |
Output:
{'Age': [18, 20, 25, 29, 30]}