Python dictionary append multiple key-values

Python Dictionary

In this post, we are going to explore how to do Python dictionary append multiple key-values with code example.

1. Python dict Append Multiple key-values

Python dictionary update() method takes a sequence as an argument, it can be, a key-value pair, dictionary, tuple, or list of tuples. We will understand how to add multiple key-value pairs in the dictionary.

1.1. Python dictionary append a list of tuples

In this code example, we are appending a list of tuples as multiple key-value pairs to the dictionary using the dictionary update() method.

#Program to Python dict append multiple values  

dict_sub = {'math':100,'Eng':100,'Chem':98}

#list of tuples 

list_of_tup = [ ('Aon',200 ) , ('Jack', 500) , ('Mack' , 600)]

#adding multiple key-value to a dictionary

dict_sub.update(list_of_tup)

print('after appending value to dictionary =\n',dict_sub)

Output after appending the list of tuples

after appending value to dictionary =
 {'math': 100, 'Eng': 100, 'Chem': 98, 'Aon': 200, 'Jack': 500, 'Mack': 600}

1.2. Append a dictionary to another dictionary

Here in this code example, we are using the update() method to adding a dictionary to another dictionary.

#Program to append multiple values to Python dictionary 

dict_sub = {'math':100,'Eng':100,'Chem':98}

#dictionary to append 

dict_stud = {'Aon':200  , 'Jack':500 , 'Mack' : 600}

#adding multiple key-value to the dictionary

dict_sub.update(dict_stud)

print('after adding value to dictionary =\n',dict_sub)

Output

after adding value to dictionary =
 {'math': 100, 'Eng': 100, 'Chem': 98, 'Aon': 200, 'Jack': 500, 'Mack': 600}

1.3 Python dictionary append Using unpacking** operator

The unpacking operator(**) merges two dictionaries and returns a new dictionary with key-value pairs from both dictionaries.

In this example, we are using the unpacking** operator to append one dictionary to another. Finally using the print() method to print the resulting output.

#Program to Python dict append multiple values 

dict_sub = {'math':100,'Eng':100,'Chem':98}

#dictionary student to append 
 
dict_stud = {'Aon':200  , 'Jack':500 , 'Mack' : 600}

#adding mutiple key-value to dictionary

dict_join = {**dict_sub,**dict_stud}

print('after adding value to dictionary =\n',dict_join)

Output

after adding value to dictionary =
 {'math': 100, 'Eng': 100, 'Chem': 98, 'Aon': 200, 'Jack': 500, 'Mack': 600}

2. Append a list of values to an existing key

We can add a list of values in the dictionary by using update() or subscript notation. Let understand with examples.

2.1. Python dict Append values Using update() method

We are appending a list of values([‘Jack’,’Aon’,’Tom’]) to an key( student) in dictionary using the update() method.

#Program to Python dict append multiple values 

dict_sub = {'math':100,'Eng':100,'Chem':98}
#adding list of values to dictionary
dict_sub.update({'student':['Jack','Aon','Tom'] })

print('after adding value to dictionary =\n',dict_sub)

Output

after adding value to dictionary =
 {'math': 100, 'Eng': 100, 'Chem': 98, 'student': ['Jack', 'Aon', 'Tom']}

2. 2. Python dict Append Using Subscript notation

we are appending a list of values([‘Jack’,’Aon’,’Tom’]) to an key( student) in dictionary using [] subscript notation.

#Program to Python dict append multiple values 

dict_sub = {'math':100,'Eng':100,'Chem':98}

#adding multiple values to a key in the dictionary

dict_sub['Student'] = ['Jack','Aon','Tom']

print('after adding value to dictionary =\n',dict_sub)

Output

after adding value to the dictionary =
 {'math': 100, 'Eng': 100, 'Chem': 98, 'Student': ['Jack', 'Aon', 'Tom']}

Conclusion

In this post,we have learnt different ways to Append Multiple key-values to Python dictionary.We can use any of them as per our requirement.