Python add dict to dict – How to Add Dictionary to Dictionary in Python?

Python add dict to dict: Here are the ways to add one dictionary to the other.

Python Dictionary:

Python add to dict: Dictionaries are Python’s implementation of a data structure that is more generally known as an associative array. A dictionary consists of a collection of key-value pairs. Each key-value pair maps the key to its associated value.Keys are unique within a dictionary while values may not be.

Adding Dictionary to Dictionary in Python

Method #1:

Approach:

  • Give the first dictionary as static input and store it in a variable
  • Give the second dictionary as static input and store it in another variable
  • Iterate in the keys of the given second dictionary using the for loop.
  • Assign the key of the second dictionary to the first dictionay.
  • Print the first dictionary (here it adds/concatenates both dictionaries).
  • The Exit of the Program.

Below is the implementation:

# Give the first dictionary as static input and store it in a variable
gvn_dict1 = {100 : 'hello', 200 : 'this', 300 : 'is', 400: 'btechgeeks'}
# Give the second dictionary as static input and store it in another variable                            
gvn_dict2 = {500:'good', 600 : 'morning'}
# Iterate in the keys of the given second dictionary using the for loop
for key in gvn_dict2:
  # Assign the key of the second dictionary to the first dictionay
  gvn_dict1[key] = gvn_dict2[key]
# Print the first dictionary.
# (here it adds/concatenates both dictionaries)
print(gvn_dict1)

Output:

{100: 'hello', 200: 'this', 300: 'is', 400: 'btechgeeks', 500: 'good', 600: 'morning'}

Method #2: Using update() Function

Approach:

  • Give the first dictionary as static input and store it in a variable
  • Give the second dictionary as static input and store it in another variable.
  • Pass the given second dictionary to the update() function and apply it on the first dictionary.
  • Here it adds given the second dictionary to the first dictionary.
  • Print the first dictionary (concatenation both dictionaries).
  • The Exit of the Program.

Below is the implementation:

# Give the first dictionary as static input and store it in a variable
gvn_dict1 = {100 : 'hello', 200 : 'this', 300 : 'is', 400: 'btechgeeks'}
# Give the second dictionary as static input and store it in another variable                            
gvn_dict2 = {500:'good', 600 : 'morning'}
# Pass the given second dictionary to the update() function and
# apply it on the first dictionary.
# Here it adds given second dictionary to the first dictionary
gvn_dict1.update(gvn_dict2)
# Print the first dictionary.
print(gvn_dict1)

Output:

{100: 'hello', 200: 'this', 300: 'is', 400: 'btechgeeks', 500: 'good', 600: 'morning'}

We can also add first dictionary to the second dictionary as shown below:

# Give the first dictionary as static input and store it in a variable
gvn_dict1 = {100 : 'hello', 200 : 'this', 300 : 'is', 400: 'btechgeeks'}
# Give the second dictionary as static input and store it in another variable                            
gvn_dict2 = {500:'good', 600 : 'morning'}
# Pass the given first dictionary to the update() function and
# apply it on the second dictionary.
# Here it adds given first dictionary to the second dictionary
gvn_dict2.update(gvn_dict1)
# Print the second dictionary.
print(gvn_dict2)

Output:

{500: 'good', 600: 'morning', 100: 'hello', 200: 'this', 300: 'is', 400: 'btechgeeks'}

Leave a Comment