Python Practice › Dictionary Programs › Program 8
Clear a dictionary
Write a Python program to remove all items from a dictionary.
Input
{"a": 1, "b": 2}Output
{} {}Approach
clear()empties the same object.
Solution
d = {"a": 1, "b": 2}alias = dd.clear()print(d, alias)Output
{} {}Line by line
Line 3
clear empties the dict itself, so every name pointing to it sees the change. d = {} would only rebind d.
Learn the concept
New to this topic? Read the lesson, then come back.