DMCA.com Protection Status
 
Search

Merge two dictionaries using for loop in Python Programming

In this blog post we are going to see program to Merge two dictionaries using for loop in Python Programming in python programming language. Also, in the last of blog post we are adding video solutions (both in English & Hindi languages) for your convenience & easy learning.

Explanation - Here we use a For loop that iterates the first dictionary and simultaneously adds entries to another dictionary to merge them.

Let's consider a program to merge the given dictionaries using the For loop.


Source Code -

print("Program by Fresher Side & Tutorial Side")
mydict1 = { 'AAM' : 21,      # given the first dictionary in key-value pairs  
            'SAM' : 19,  
            'JAM' : 26,  
            'PAM' : 22  
                        }   
mydict2 = {  
            'Orange' : 29,      # given the second dictionary in key-value pairs  
            'Maria' : 26,  
            'Lemon' : 00  
}                          
print("Before merging the two dictionary ")  
print("Dictionary No. 1 is : ", mydict1) # print the dict1  
print("Dictionary No. 2 is : ", mydict2)  # print the dict2  
  
mydict3 = mydict1.copy()  # Copy the dict1 into the dict3 using copy() method  
  
for key, value in mydict2.items():  # use for loop to iterate dict2 into the dict3 dictionary  
    mydict3[key] = value  
  
print("After merging of the two Dictionary ")  
print(mydict3)    # print the merge dictionary  

Suitable for Online Compilers/Code Blocks/VS Code & Most of the Compilers.


Output -

Program by Fresher Side & Tutorial Side
Before merging the two dictionary 
Dictionary No. 1 is :  {'AAM': 21, 'SAM': 19, 'JAM': 26, 'PAM': 22}
Dictionary No. 2 is :  {'Orange': 29, 'Maria': 26, 'Lemon': 0}
After merging of the two Dictionary 
{'AAM': 21, 'SAM': 19, 'JAM': 26, 'PAM': 22, 'Orange': 29, 'Maria': 26, 'Lemon': 0}

Video Explanation (English)


Video Explanation (Hindi)


If you have any doubt feel free to comment.

For Job/Internship Updates - Click Here

Follow us on Instagram

Follow us on LinkedIn

Join us on Telegram

Subscribe our Youtube Channel (Hindi)

Subscribe our Youtube Channel (English)

Download our Android App - Fresher Side Updates

Related Posts

See All
  • Facebook
  • Instagram
  • LinkedIn
  • 1024px-Telegram_logo.svg
  • download
  • YouTube
  • Google Play