Python dictionary extend [4+ examples]

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 program, 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 given code

{'james': 18, 'potter': 67, 'George': 12, 'Micheal': 91}
python dict expansion
Python dictionary extend

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:

{'m': 51, 'q': 82, 'k': 10, 'u': 14}
Python dictionary extend
Python dictionary extend

This is how to extend a dictionary in Python.

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 concatenate 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 program, we will learn about the update() function and check how to add elements to a given dictionary. In a Python dictionary, the update() function is used to update the dictionary with the elements in the form of a Key-value pair from another dictionary.
  • In Python dictionary, append can be used by list as a list, we can also use the “+” operator to append the lists of each key within a dictionary.

Example:

Let’s an example and check the combination of the update() and append() functions in a 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

Update elements {'Micheal': 178, 'George': 923, 'Potter': 236}
Append elements {'Germany': [8, 'France']}
python expand dict
Python dictionary extend vs append

Python dictionary extend list

  • Here we can see how to update the value list in a Python dictionary.
  • By using the list comprehension method we can update the value list to extract a particular key and then iterate over its 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:

Updated dictionary: {'student_id': 678, 'student_val': [36, 8, 4], 'age': 19}
extend python dictionary
Python dictionary extend the list

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 a key-value pair.
  • In this example, we can apply the subclasses like dict and use built-in keywords 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 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 assigned ‘self’ and ‘*args’ as an argument.

Here is the implementation of the following code:

original dictionary: {'m': 4, 'z': 7}
Updated dictionary: {'m': 4, 'z': 7, 'u': 15, 'w': 19}
extend a dictionary python
Python extend dictionary class

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 a 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 a 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)

Output: 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.

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

You can refer to the below screenshot:

how to extend a dictionary in python
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:

NewLanguages {'Mongodb': 800, 'Sql': 892, 'C++': 956}
updated language {'Mongodb': 800, 'Sql': 892, 'C++': 956, 'java': 300, 'Pascal': 890}
python extend dictionary with another
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: