Python Practice › Dictionary Programs › Program 3
Move items to another dictionary
Write a Python program to move all items from dict1 to dict2, leaving dict1 empty.
Input
{"name": "john", "city": "London", "country": "UK"}Output
dict1 = {}
dict2 = {'name': 'john', 'city': 'London', 'country': 'UK'}Approach
updatedict2, thencleardict1.
Solution
dict1 = {"name": "john", "city": "London", "country": "UK"}dict2 = {}dict2.update(dict1)dict1.clear()print("dict1 =", dict1)print("dict2 =", dict2)Output
dict1 = {}
dict2 = {'name': 'john', 'city': 'London', 'country': 'UK'}Learn the concept
New to this topic? Read the lesson, then come back.