Clear a dictionary

EasyDictionary Programs · 8 of 94

Write a Python program to remove all items from a dictionary.

Advertisement
Input
{"a": 1, "b": 2}
Output
{} {}

Approach

  1. clear() empties the same object.

Solution

Python
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.

Advertisement

Learn the concept

New to this topic? Read the lesson, then come back.

More dictionary programs

← All 94 Dictionary Programs