Clear entire dictionary using clear() Dry Run in PYTHON

Clear entire dictionary using clear() is an interactive PYTHON dry run visualizer from the Dict programs section. Study the source code, then use the execution controls to follow each step, variable update, highlighted line, and console output.

This page provides a browser-based dry run with source-code highlighting, auto-scroll, voice narration controls, and execution output for learning the program step by step.

Clear entire dictionary using clear() Program Code

# Clear entire dictionary using clear()
settings = {"theme": "dark", "font": "Arial", "size": 12, "notifications": True}
print("Before clear:", settings)
print(f"Length: {len(settings)}")

# clear() removes all key-value pairs
settings.clear()
print("\nAfter clear():", settings)
print(f"Length: {len(settings)}")

# Dictionary still exists (empty)
print(f"Type after clear: {type(settings)}")

# Can add new items after clear
settings["new_key"] = "new_value"
print(f"\nAfter adding new item: {settings}")

# clear() vs reassigning
original = {"a": 1, "b": 2}
original.clear()  # Modifies existing dict
print(f"\nAfter clear(): {original}")

# Reassigning creates new dict
another = {"x": 10, "y": 20}
another = {}  # New empty dict, old one may be garbage collected
print(f"After reassign: {another}")

View the complete Dict programs page.

Program Console Clear entire dictionary using clear() Topic: PYTHON Vignaankosh.com
Execution Panel
Step 0/0
Console is empty.