How to Append Values to Dictionary in Python?

There are a number of important data types available in Python, including dictionaries. The dictionary data type is utilized to keep the data in a key/value pair format. In Python appending a dictionary means adding new key-value pairs or updating existing ones.

To organize and manipulate data in Python, the values are appended into the dictionary using various methods. In this post we will provide various methods to append the values to the dictionary in Python:

Method 1: Using Square Brackets

To access and assign the value the square bracket is used along with the dictionary variable. The following code uses the square bracket to append the values to the dictionary:

Code: 

dict_value = {'Name': 'Alex', 'Age': 17, 'Height': 5.7}
print("Original Dictionary:", dict_value)
dict_value['Salary'] = 941
dict_value['Id-No'] = 180445
print("\nDictionary After Appending:", dict_value)

The dictionary variable name is used along with square brackets “[ ]” and the assignment operator “=” to append the specific value to the given dictionary.

Output: 

The given values have been appended to the dictionary.

Method 2: Using dict() Constructor

The “dict()” constructor is used to converting any Python object into a dictionary object. The following code uses the “dict()” function to append values to the dictionary:

Code: 

dict_value = {'Name': 'Alex', 'Age': 17, 'Height': 5.7}
print("Original Dictionary:", dict_value)
dict_value = dict(dict_value, Salary=941)
print("\nDictionary After Appending:", dict_value)

The “dict()” function takes the dictionary and new specific value as an argument and appends them into the dictionary.

Output: 

The specified value has been added to the dictionary.

Method 3: Using dict.update()

The “dict.update()” function is utilized to update the dictionary element with a new key-value pair if the key-value pair is not present/exists in the dictionary. The following code uses this method to append values to the dictionary:

Code: 

dict_value = {'Name': 'Alex', 'Age': 17, 'Height': 5.7}
print("Original Dictionary:", dict_value)
dict_value.update({'Salary': 962})
print("\nDictionary After Appending:", dict_value)

The “dict.update()” function takes the key-value pair as an argument and inserts them into the dictionary.

Output: 

The specified key-value pair has been added to the dictionary.

Method 4: Using for Loop

In the below code, the for loop is utilized to iterate over the given values and append them to the input dictionary.

Code: 

dict_value = {'Name': 'Alex', 'Age': 17, 'Height': 5.7}
print("Original Dictionary:", dict_value)
new_value = [('Name_1', 'Lily'), ('Name_2', 'Joseph')]
for key, value in new_value:
    dict_value[key] = value
print("\nDictionary After Appending:", dict_value)

The for loop iterates over the list of tuples and fetches the new value. The new value is appended to the dictionary inside the for loop block using the expression “dict_value[key] = value”.

Output: 

The values have been added successfully.

Method 5: Using Unpacking Operator in Dictionary Comprehension

The unpacking operator is used to unpack the dictionary iterable and return them as an individual element. The unpacking operator is used to append the specific values to the dictionary :

Code: 

dict_value = {'Name': 'Alex', 'Age': 17, 'Height': 5.7}
print("Original Dictionary:", dict_value)
dict_value = {**dict_value, **{'Salary': 962}}
print("\nDictionary After Appending:", dict_value)

In the dictionary comprehension the new dictionary is created with one key-value pair i.e. {‘salary’: 962}. After that, the unpacking operator is used to merge two dictionaries by unpacking their key-value pairs.

Output: 

The given values have been appended to the input dictionary.

Method 6: Using append() Function

In Python, the “append()” function is used to add an item to the end of a list. The following code appends the values to the specific key value list:

Code: 

dict_value = {'Name': ['Alex'], 'Age': 17, 'Height': 5.7}
print("Original Dictionary:", dict_value)
dict_value['Name'].append('Joseph')
dict_value['Name'].append('Lily')
print("\nDictionary After Appending:", dict_value)

The “append()” function takes the new value as an argument and appends it to the dictionary key-value list.

Output: 

The multiple elements of the dictionary key value list have been added.

Method 7: Using extend() Function

The “extend()” function is utilized to add all the item values of the given iterable to the end of the list. The below code uses the “extend()” function to append the values to the dictionary key list:

Code: 

dict_value = {'Name': ['Alex'], 'Age': 17, 'Height': 5.7}
print("Original Dictionary:", dict_value)
dict_value['Name'].extend(['Joseph', 'David'])
print("\nDictionary After Appending:", dict_value)

The “extend()” function accepts the multiple values as an argument and appends them to the dictionary key-value list.

Output: 

The values have been added to the dictionary key-value list.

Conclusion

To append values to the dictionary, the square bracket, dict(), dict.update(), for loop, unpacking operator, append(), and extend() functions are used in Python. The “dict()” function takes the dictionary and new key-value pair as an argument and inserts/appends them into the end of the dictionary. While the “for loop”, “dict.update()” and other methods can be efficient ways to append the values to the dictionary. 

This guide has presented an in-depth guide on appending values to the dictionary using various examples.