In python, the dictionary function clear() is used to remove all the items from a dictionary. In this tutorial we’ll look at its syntax and usage along with some examples.
Before we proceed, here’s a quick refresher on dictionaries in python – Dictionaries are a collection of items used for storing key to value mappings. They are mutable and hence we can update the dictionary by adding new key-value pairs, removing existing key-value pairs, or changing the value corresponding to a key. For more, check out our guide on dictionaries and other data structures in python.
How to remove all the items from a dictionary?
The python dictionary function clear() can be used to remove all the items from a dictionary. Example, d.clear()
. You can also set the dictionary to an empty dictionary d = {}
. The difference is, d.clear()
modifies the dictionary in-place while d = {}
creates a new empty dictionary and assigns it to d
.
The clear() function
The following is the syntax for using the clear()
function:
sample_dict.clear()
Here, sample_dict is the dictionary you want to clear out.
Parameters: The clear()
function does not take any parameters.
Returns: It does not return any value (It returns None
). It modifies the list in-place, meaning the original dictionary gets modified.
Example: Use the clear function to remove a dictionary’s items
Introductory ⭐
- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science
Intermediate ⭐⭐⭐
- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization
Advanced ⭐⭐⭐⭐⭐
- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science
🔎 Find Data Science Programs 👨💻 111,889 already enrolled
Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.
# dictionary of a sample portfolio
shares = {'APPL': 100, 'GOOG': 50, 'MSFT': 200}
# print the dictionary
print("Shares:", shares)
# clear the dictionary
returned_val = shares.clear()
# print the dictionary
print("Shares:", shares)
print("The returned value:", returned_val)
Output:
Shares: {'APPL': 100, 'GOOG': 50, 'MSFT': 200}
Shares: {}
The returned value: None
In the above example, the clear()
dictionary function is used to remove all the items from the dictionary shares
. On printing the dictionary after the function call we see the original dictionary cleared out. As for the returned value, we see that it doesn’t actually return a value, it returns None
.
For more on python dictionary functions, refer to the python docs.
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.