Write a python program to merge two python dictionaries ..into one?

def merge_dicts(dict1, dict2):
return {**dict1, **dict2}

dict1 = {‘a’: 1, ‘b’: 2}
dict2 = {‘c’: 3, ‘d’: 4}

merged_dict = merge_dicts(dict1, dict2)
print(merged_dict)

3 Likes

Mam pls modify my code…
To take input from user…

1 Like

@sayanalu9091

correct , keep it up.
Keep practising.

I reply to you with the user input code for this question.

@sayanalu9091

dictionary = {} --------Empty dictionary

for i in range(3): --------------------you can take whatever range you want
key = input("Enter key: ")
value = input("Enter value: ")

dictionary[key] = value
print(dictionary)

In the above way you can take the user input for a dictionary.

s1 = {‘a’:10, ‘b’:“data”, ‘c’:5.4}
s2 = {‘value’: 3, ‘m’:10, ‘da’: 6}

def merge(dict1,dict2):
dict2 =(dict2.update(dict1))
return dict2
merge(s1,s2)
print(s2)

@sindhiyadevit
Correct, keep it up