w3resource logo


Python Exercises

Python Exercise: Program to concatenate dictionaries to create a new one


Write a Python program to concatenate following dictionaries to create a new one.

Sample Dictionary :
dic1={1:10, 2:20}
dic2={3:30, 4:40}
dic3={5:50,6:60}
Expected Result : {1: 10, 2: 20, 3: 30, 4: 40, 5: 50, 6: 60}

Sample Solution :

Python Code :

  1. dic1={1:102:20}  
  2. dic2={3:304:40}  
  3. dic3={5:50,6:60}  
  4. dic4 = {}  
  5. for d in (dic1, dic2, dic3): dic4.update(d)  
  6. print(dic4)  

Console :

Copy and paste the above code and press "Enter key" to execute :

Post your code through Disqus