In this article we will discuss how to add or append new key value pairs in a dictionary and also how to update value of existing keys.

Python dictionary provides a member function update() i.e.

dict.update(Iterable_Sequence of key:value)

It accepts in iterable sequence of key value pairs that can be single key value pair or list of tuples or an another dictionary. For each entry in sequence, it will add the given key value pair in dictionary and if key already exists then it will update its value.

Let’s use this to add new key value pairs in dictionary or updating existing one.

Suppose we have dictionary of string and int i.e.

# Dictionary of strings and ints
wordFreqDic = {
    "Hello": 56,
    "at" : 23 ,
    "test" : 43,
    "this" : 43
    }

Append a new key value pair in dictionary

# Adding a new key value pair
wordFreqDic.update( {'before' : 23} )

It will add a new key value pair in the dictionary. Dictionary contents will be now
test  ::  43
this  ::  43
Hello  ::  56
at  ::  23
before  ::  23

If key is string you can directly add without curly braces i.e.
# Adding a new key value pair
wordFreqDic.update(test = 'value' )

Updating value of existing key in dictionary

If call the update() function with a key/value and key already exists in dictionary then its value will be updated by new value i.e

Key ‘Hello’ already exist in dictionary i.e.

this  ::  43
at  ::  23
test  ::  value
Hello  ::  56
before  ::  23

Now let’s call update() on key ‘Hello’ i.e.
# Updating existing key's value
wordFreqDic.update(Hello = 99 )

Now value of key ‘Hello’ will be updated and contents of dictionary will be,
this  ::  43
at  ::  23
test  ::  value
Hello  ::  99
before  ::  23

Append multiple key value pair in dictionary

As update() accepts an iterable sequence of key value pairs, so we can pass a dictionary or list of tuples of new key value pairs to update(). It will all add the given key value pairs in the dictionary, if any key already exists then it will update its value.

Adding a list of tuples (key value pairs) in dictionary

# Adding a List of tuples to the existing dictionary
wordFreqDic.update([ ('where', 4) , ('who', 5) , ('why', 6) , ('before' , 20)] )

Dictionary contents will be now,
where  ::  4
this  ::  43
at  ::  23
test  ::  value
Hello  ::  99
who  ::  5
why  ::  6
before  ::  20

Adding a dictionary to another dictionary

Suppose we have two dictionary i.e.

# Two dictionaries
dict1 = {
    "Hello": 56,
    "at" : 23 ,
    "test" : 43,
    "this" : 43
    }
dict2 = { 'where' : 4 ,
     'who' : 5 ,
     'why': 6 ,
     'this' : 20 
     }

Let’s add the contents of dict2 in dict1 i.e.
# Adding elements from dict2 to dict1
dict1.update( dict2 )

Contents of dict1 will be now,
Hello  ::  56
at  ::  23
where  ::  4
this  ::  20
test  ::  43
who  ::  5
why  ::  6

Complete Series of Python Dictionary tutorials,

Subscribe with us to join a list of 2000+ programmers and get latest tips & tutorials at your inbox through our weekly newsletter.

Complete example is as follows,

def main():
    
    # Dictionary of strings and ints
    wordFreqDic = {
        "Hello": 56,
        "at" : 23 ,
        "test" : 43,
        "this" : 43
        }
    
    
    print("Original Dict : " , wordFreqDic)
    

    '''
    Adding a new key value pair in dictionary
    '''

    print("Appending key value pair in dictionary")
        
    # Adding a new key value pair
    wordFreqDic.update( {'before' : 23} )
    
    print("Modified Dict : ")
    for (key, value) in wordFreqDic.items() :
        print(key , " :: ", value )
    
    # Adding a new key value pair
    wordFreqDic.update(test = 'value' )
    
    print("Modified Dict : ")
    for (key, value) in wordFreqDic.items() :
        print(key , " :: ", value )

    '''
    Updating value of existing key in dictionary
    '''
    print("Updating values in dictionary")
    
    # Updating existing key's value
    wordFreqDic.update(Hello = 99 )
    
    print("Modified Dict : ")
    for (key, value) in wordFreqDic.items() :
        print(key , " :: ", value )


    
    '''
    Adding multiple key value pair in dictionary
    '''
    print("Append list of tuples in dictionary")
    
    # Adding a List of tuples to the existing dictionary
    wordFreqDic.update([ ('where', 4) , ('who', 5) , ('why', 6) , ('before' , 20)] )
    
    print("Modified Dict : ")
    for (key, value) in wordFreqDic.items() :
        print(key , " :: ", value )


    print("Append one dictionary to another dictionary")    
    
    # Two dictionaries
    dict1 = {
        "Hello": 56,
        "at" : 23 ,
        "test" : 43,
        "this" : 43
        }
    dict2 = { 'where' : 4 ,
         'who' : 5 ,
         'why': 6 ,
         'this' : 20 
         }
    
   
    # Adding elements from dict2 to dict1
    dict1.update( dict2 )
    
    print("Modified Dict1 : ")
    for (key, value) in dict1.items() :
        print(key , " :: ", value )


if __name__ == '__main__':
    main()


Output:
Original Dict :  {'this': 43, 'test': 43, 'at': 23, 'Hello': 56}
Appending key value pair in dictionary
Modified Dict : 
before  ::  23
this  ::  43
test  ::  43
at  ::  23
Hello  ::  56
Modified Dict : 
before  ::  23
Hello  ::  56
this  ::  43
test  ::  value
at  ::  23
Updating values in dictionary
Modified Dict : 
before  ::  23
Hello  ::  99
this  ::  43
test  ::  value
at  ::  23
Append list of tuples in dictionary
Modified Dict : 
before  ::  20
where  ::  4
Hello  ::  99
why  ::  6
this  ::  43
test  ::  value
at  ::  23
who  ::  5
Append one dictionary to another dictionary
Modified Dict1 : 
where  ::  4
Hello  ::  56
why  ::  6
this  ::  20
test  ::  43
at  ::  23
who  ::  5