Skip to content

Merge two dictionaries into one in Python

Advertisements

dictionary1 = {'name': 'Anand', 'age': 16, 'sex': 'Male'}
dictionary2 = {'class': 'X', 'score': 88}

def mergeDicts(*dicts):
  res = dict()
  for d in dicts:
    res.update(d)
  return res

print(mergeDicts(dictionary1,dictionary2))

#{'class': 'X', 'age': 16, 'score': 88, 'name': 'Anand', 'sex': 'Male'}

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.