Python dictionary extend – Complete tutorial

In this Python tutorial, we will discuss the Python dictionary extend. Here we will also cover the below examples:

  • Python dictionary extend vs append
  • Python dictionary extend list
  • Python extend dictionary class
  • Python dictionary extend function
  • Python extend dictionary with another
  • Python dictionary extend vs update

Python dictionary extend

  • In this programm we will see how to extend a dictionary in Python.
  • In Python, the dict.update() will help the user to extend a dictionary with the key-value pair from one dictionary to another.

Example:

Let’s take an example and check how to extend a dictionary in Python.

my_dict1 = {"james": 18, "potter": 67}

new_dictionary = {"George": 12, "Micheal": 91}
my_dict1.update(new_dictionary)
print(my_dict1)

In the above example first, we initialize two dictionaries ‘my_dict1’ and ‘new_dictionary’ which contain elements in the form of key-value pair.

Now we have to use dict.update() method to extend the dictionary into another one.

Here is the execution of the following give code

Python dictionary extend
Python dictionary extend

Read: How to delete a Dictionary in Python

How to extend a dictionary in Python

By using the ** operator we can perform this particular task and this method adds the specified dictionary elements to the end of the current dictionary.

Source Code:

my_dict = {'m': 51, 'q': 82}
z = {'k': 10, 'u': 14}

d = {**my_dict, **z}
print(d)

Output:

Python dictionary extend
Python dictionary extend

This is how to extend a dictionary in Python.

Read Python dictionary append with examples

Python dictionary extend vs append

  • In Python dictionary, the append() function is used to add elements to the keys in the dictionary. In the dictionary extend() means to concatentates the first dictionary with another dictionary.
  • In Python dictionary, the append() function is used to add elements to the keys in the dictionary. In the dictionary extend() means to concatenate the first dictionary with another dictionary.
  • In this programm, we will learn about the update() function and check how to add elements in a given dictionary. In a Python dictionary, the update() function is used to update the dictionary with the elements in the form of Key-value pair from another dictionary.
  • In Python dictionary, append can be used by list as a list, we can also use “+” operator to append the lists of each key within a dictionary.

Example:

Let’s an example and check the combination of update and append() function Python dictionary

student_name = {'Micheal':178, 'George':923}
add_new = {'Potter':236}

student_name.update(add_new)
print("Update elements",student_name)

new_country = {'Germany': 'France'}
new_country['Germany'] = [8]
new_country['Germany'].append('France')
print("Append elements",new_country)

After writing the above code, you will see first we initialize a dictionary that stores key-value pair elements. Here the update() function is used to add the element in the dictionary and call the print statement to display the output.

After that create another dictionary and use the append function to insert the elements in the dictionary.

Here is the execution of the following given code

Python dictionary extend vs append
Python dictionary extend vs append

Read How to convert dictionary to JSON in Python

Python dictionary extend list

  • Here we can see how to update the value list in a Python dictionary.
  • By using list comprehension method we can update the value list and extract a particular key and then iterate over it’s value in this method.

Source Code:

stud_dictionary = {'student_id' :678, 'student_val' : [9,2,1], 'age' : 19}

stud_dictionary['student_val'] = [y * 4 for y in stud_dictionary['student_val']]
print("Updated dictionary:",stud_dictionary)

In the above code, once you will print “stud_dictionary” then the result will display as a “student_val”:[36,8,4]. In this program, the “*” operator is used to update the values from a list. You can check the below screenshot for updating the value list.

Output:

Python dictionary extend list
Python dictionary extend the list

Read Python Concatenate Dictionary

Python extend dictionary class

  • In this program, we will see how to extend a Python dictionary class whose instances will keep the data contained by their element in the form of key-value pair.
  • In this example we can apply the subclasses like dict and use built-in keyword like ‘self’ and ‘super’.

Example:

class my_new_dict(dict):
    def __init__(self, *b, **a):
        super(my_new_dict,self).__init__(*b, **a)
        self.itemlist = super(my_new_dict,self).keys()
    def __setitem__(self, new_k, new_va):
    
        set(self.itemlist).add(new_k)
        super(my_new_dict,self).__setitem__(new_k, new_va)
    def __iter__(self):
        return iter(self.itemlist)

 
new_val = my_new_dict(m=4,z=7)
print("original dictionary:",new_val)
new_val['u']=15
new_val['w']=19
print ("Updated dictionary:",new_val)   

Note: In this example dict.keys() method does not return a list so we apply the ‘self. item list’ method.

In the above code, once you will print “new_val” then the output will display as a ‘{‘m’:4,’z’:7,’u’:8}’. In this program, we use the function ‘_init_’ in which we have assign ‘self’ and ‘*args’ as an argument.

Here is the implementation of the following given code

Python extend dictionary class
Python extend dictionary class

Read Python Dictionary update with examples

Python dictionary extend function

Note: In the Python dictionary, there is no built-in extend() function which we can apply in a dictionary. So in this case we can apply the update () function and it will help the user to update the dictionary with the elements in the form of key-value pair from another dictionary.

Python extend dictionary with another

  • Let us see how to add a dictionary with another dictionary in Python.
  • By using the ** operator we can solve this task and this method inserts the specified dictionary elements in the form of key-value pair to the end of the current dictionary.

Example:

my_new_org_dict = {'John': 782, 'Micheal': 173}
update_dict = {'George': 192, 'oliva': 967}

new_val = {**my_new_org_dict, **update_dict}
print(new_val)  

In the above code first, we will initialize a dictionary called ‘my_new_org_dict’ that stores elements and then use another dictionary ‘update_dict’.

Now in this program, we use the operator ‘**’ for appending elements. Here we can see that the dictionary is added to another dictionary.

You can refer to the below screenshot:

Python extend dictionary with another
Python extend dictionary with another

Python dictionary extend vs update

  • Here we will discuss the difference between the extend and update method in Python.
  • In Python, there is no extend() function in the dictionary but we can apply this method in lists and tuples. In Python, the update() function is used to modify the dictionary and by using this method we can concatenate the old dictionary to a new dictionary.

Example:

my_new_dict = {'Mongodb':800,'Sql':892,'C++':956}

print("NewLanguages",my_new_dict)
my_new_dict.update({'java':300,'Pascal':890})
print("updated language",my_new_dict)

Here is the Output of the following given code

Python dictionary extend vs update
Python dictionary extend vs update

In this Python tutorial, we will discuss the Python dictionary extend. Here we will also cover the below examples:

  • Python dictionary extend vs append
  • Python dictionary extend list
  • Python extend dictionary class
  • Python dictionary extend function
  • Python extend dictionary with another
  • Python dictionary extend vs update

Related Posts: