Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,900 questions

51,831 answers

573 users

How to add two dictionaries to new dictionary in Python

5 Answers

0 votes
language1 = {'python': 13, 'php': 50, 'java': 6}
language2 = {'c++': 2, 'c#': 87, 'javascript': 52}

new_dic = dict(language1)
new_dic.update(language2)

print(new_dic)


'''
run:

{'python': 13, 'php': 50, 'java': 6, 'c++': 2, 'javascript': 52, 'c#': 87}
 
'''

 



answered Aug 29, 2018 by avibootz
0 votes
language1 = {'python': 13, 'php': 50, 'java': 6}
language2 = {'c++': 2, 'c#': 87, 'javascript': 52}

new_dic = language1.copy()
new_dic.update(language2)

print(new_dic)


'''
run:

{'php': 50, 'python': 13, 'javascript': 52, 'c++': 2, 'java': 6, 'c#': 87}
 
'''

 



answered Aug 29, 2018 by avibootz
0 votes
language1 = {'python': 13, 'php': 50, 'java': 6}
language2 = {'c++': 2, 'c#': 87, 'javascript': 52}
 
new_dic = {**language1, **language2}

print(new_dic)
 
 
'''
run:
 
{'php': 50, 'c++': 2, 'java': 6, 'python': 13, 'c#': 87, 'javascript': 52}
  
'''

 



answered Aug 29, 2018 by avibootz
0 votes
from itertools import chain

language1 = {'python': 13, 'php': 50, 'java': 6}
language2 = {'c++': 2, 'c#': 87, 'javascript': 52}

new_dic = dict(chain(language1.items(), language2.items()))

print(new_dic)


'''
run:

{'javascript': 52, 'php': 50, 'c++': 2, 'python': 13, 'java': 6, 'c#': 87}
  
'''

 



answered Aug 29, 2018 by avibootz
0 votes
language1 = {'python': 13, 'php': 50, 'java': 6}
language2 = {'c++': 2, 'c#': 87, 'javascript': 52}

new_dic = dict(list(language1.items()) + list(language2.items()))

print(new_dic)


'''
run:

{'php': 50, 'java': 6, 'python': 13, 'c++': 2, 'javascript': 52, 'c#': 87}
  
'''

 



answered Aug 29, 2018 by avibootz
...