Computer Science
Write a Python script to merge two Python dictionaries.
Python
Python Dictionaries
3 Likes
Answer
dict1 = eval(input("Enter the first dictionary: "))
dict2 = eval(input("Enter the second dictionary: "))
dict1.update(dict2)
print("Merged dictionary:", dict1)
Output
Enter the first dictionary: {1 : "Orange", 2: "Mango", 3 : "Watermelon"}
Enter the second dictionary: {4 : "Pineapple", 5 : "Banana"}
Merged dictionary: {1: 'Orange', 2: 'Mango', 3: 'Watermelon', 4: 'Pineapple', 5: 'Banana'}
Answered By
1 Like
Related Questions
Write a Python script to check if a given key already exists in a dictionary.
Write a Python script to print a dictionary where the keys are numbers between 1 and 15 (both included) and the values are square of keys.
Sample Dictionary
{1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64, 9: 81, 10: 100, 11: 121, 12: 144, 13: 169, 14: 196, 15: 225}
Write a Python program to sort a dictionary by key.
Write a Python program to combine two dictionaries adding values for common keys.
d1 = {'a': 100, 'b': 200, 'c':300} d2 = {'a': 300, 'b': 200, 'd':400}
Sample output:
Counter({'a': 400, 'b': 400, 'c': 300, 'd': 400})