• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
PythonForBeginners.com

PythonForBeginners.com

Learn By Example

  • Home
  • Learn Python
    • Python Tutorial
  • Categories
    • Basics
    • Lists
    • Dictionary
    • Code Snippets
    • Comments
    • Modules
    • API
    • Beautiful Soup
    • Cheatsheet
    • Games
    • Loops
  • Python Courses
    • Python 3 For Beginners
You are here: Home / Basics / Empty a Dictionary in Python

Empty a Dictionary in Python

Author: Aditya Raj
Last Updated: March 3, 2022

We use a python dictionary to store key-value pairs. Sometimes, we might need to delete key-value pairs from a dictionary. In this article, we will discuss different ways to empty a dictionary by deleting all the key-value pairs from a dictionary in python.

Empty a Dictionary using the pop() method

The pop() method is used to delete a key-value pair from a dictionary. This method, when invoked on a dictionary, takes the key as its first input argument and an optional default value as its second input argument. Upon execution, it deletes the key and its associated value from the dictionary and returns the value as shown below.

myDict = {1: 1, 2: 4, 3: 9, 4: 16, 5: 25}
print("The input dictionary is:")
print(myDict)
myDict.pop(1)
print("The output dictionary is:")
print(myDict)

Output:

The input dictionary is:
{1: 1, 2: 4, 3: 9, 4: 16, 5: 25}
The output dictionary is:
{2: 4, 3: 9, 4: 16, 5: 25}

If the key does not exist in the dictionary, the pop() method returns the default value that is passed to it as the second argument. 

myDict = {1: 1, 2: 4, 3: 9, 4: 16, 5: 25}
print("The input dictionary is:")
print(myDict)
val = myDict.pop(10, -1)
print("The popped value is:", val)
print("The output dictionary is:")
print(myDict)

Output:

The input dictionary is:
{1: 1, 2: 4, 3: 9, 4: 16, 5: 25}
The popped value is: -1
The output dictionary is:
{1: 1, 2: 4, 3: 9, 4: 16, 5: 25}

If we do not pass any default value to the pop() method and the key does not exist in the dictionary, the pop() method raises the KeyError exception as follows.

myDict = {1: 1, 2: 4, 3: 9, 4: 16, 5: 25}
print("The input dictionary is:")
print(myDict)
val = myDict.pop(10)
print("The output dictionary is:")
print(myDict)

Output:

The input dictionary is:
{1: 1, 2: 4, 3: 9, 4: 16, 5: 25}
Traceback (most recent call last):
  File "/home/aditya1117/PycharmProjects/pythonProject/string1.py", line 4, in <module>
    val = myDict.pop(10)
KeyError: 10

You can avoid the exception by using python try except blocks.

To empty the dictionary using the pop() method, we will first extract the keys of the dictionary using the keys() method. After that, we will delete each key and its associated value from the dictionary as follows.

myDict = {1: 1, 2: 4, 3: 9, 4: 16, 5: 25}
print("The input dictionary is:")
print(myDict)
key_list = list(myDict.keys())
for key in key_list:
    myDict.pop(key)
print("The output dictionary is:")
print(myDict)

Output:

The input dictionary is:
{1: 1, 2: 4, 3: 9, 4: 16, 5: 25}
The output dictionary is:
{}

Empty a Dictionary using the del statement

Instead of the pop() method, we can use the del statement in python to empty a dictionary in python. In this approach, we will delete each key-value pair in the dictionary using the keys of the dictionary as follows.

myDict = {1: 1, 2: 4, 3: 9, 4: 16, 5: 25}
print("The input dictionary is:")
print(myDict)
key_list = list(myDict.keys())
for key in key_list:
    del myDict[key]
print("The output dictionary is:")
print(myDict)

Output:

The input dictionary is:
{1: 1, 2: 4, 3: 9, 4: 16, 5: 25}
The output dictionary is:
{}

Empty a Dictionary using the clear() method

Using the clear() method, we can empty a dictionary in python in just a single statement. The clear() method, when invoked on a dictionary deletes all the key-value pairs in the dictionary. This leaves us with an empty dictionary as you can observe in the following example.

myDict = {1: 1, 2: 4, 3: 9, 4: 16, 5: 25}
print("The input dictionary is:")
print(myDict)
myDict.clear()
print("The output dictionary is:")
print(myDict)

Output:

The input dictionary is:
{1: 1, 2: 4, 3: 9, 4: 16, 5: 25}
The output dictionary is:
{}

Conclusion

In this article, we have discussed three ways to empty a dictionary in python. To know more about dictionaries in python, you can read this article on dictionary comprehension in python. You might also like this article on list comprehension in python.

Related

Recommended Python Training

Course: Python 3 For Beginners

Over 15 hours of video content with guided instruction for beginners. Learn how to create real world applications and master the basics.

Enroll Now

Filed Under: Basics Author: Aditya Raj

More Python Topics

API Argv Basics Beautiful Soup Cheatsheet Code Code Snippets Command Line Comments Concatenation crawler Data Structures Data Types deque Development Dictionary Dictionary Data Structure In Python Error Handling Exceptions Filehandling Files Functions Games GUI Json Lists Loops Mechanzie Modules Modules In Python Mysql OS pip Pyspark Python Python On The Web Python Strings Queue Requests Scraping Scripts Split Strings System & OS urllib2

Primary Sidebar

Menu

  • Basics
  • Cheatsheet
  • Code Snippets
  • Development
  • Dictionary
  • Error Handling
  • Lists
  • Loops
  • Modules
  • Scripts
  • Strings
  • System & OS
  • Web

Get Our Free Guide To Learning Python

Most Popular Content

  • Reading and Writing Files in Python
  • Python Dictionary – How To Create Dictionaries In Python
  • How to use Split in Python
  • Python String Concatenation and Formatting
  • List Comprehension in Python
  • How to Use sys.argv in Python?
  • How to use comments in Python
  • Try and Except in Python

Recent Posts

  • Count Rows With Null Values in PySpark
  • PySpark OrderBy One or Multiple Columns
  • Select Rows with Null values in PySpark
  • PySpark Count Distinct Values in One or Multiple Columns
  • PySpark Filter Rows in a DataFrame by Condition

Copyright © 2012–2025 · PythonForBeginners.com

  • Home
  • Contact Us
  • Privacy Policy
  • Write For Us