×

Python Tutorial

Python Basics

Python I/O

Python Operators

Python Conditions & Controls

Python Functions

Python Strings

Python Modules

Python Lists

Python OOPs

Python Arrays

Python Dictionary

Python Sets

Python Tuples

Python Exception Handling

Python NumPy

Python Pandas

Python File Handling

Python WebSocket

Python GUI Programming

Python Image Processing

Python Miscellaneous

Python Practice

Python Programs

Python Dictionary clear() Method (with Examples)

By IncludeHelp Last updated : December 21, 2024

In this tutorial, we will learn about the clear() method of a dictionary with its usage, syntax, parameters, return type, and examples.

Python Dictionary clear() Method

The clear() is an inbuilt method of dict class that is used to clear the dictionary i.e., to remove all elements of the dictionary. The method is called with this dictionary and returns none.

Syntax

The following is the syntax of clear() method:

dictionary_name.clear()

Parameter(s):

The following are the parameter(s):

  • None

Return Value

The return type of this method is <class 'NoneType'>, it returns nothing.

Example 1: Use of Dictionary clear() Method

# dictionary declaration
student = {
  "roll_no": 101,
  "name": "Shivang",
  "course": "B.Tech",
  "per" : 98.5
}

# printing dictionary
print("data before clearing...")
print(student)

# clearing dictionary
student.clear()

# printing dictionary after clear()
print("data after clearing...")
print(student)

Output

data before clearing...
{'course': 'B.Tech', 'roll_no': 101, 'per': 98.5, 'name': 'Shivang'}
data after clearing...
{}

Example 2: Use of Dictionary clear() Method

# dictionary declaration
data = {"a": 1, "c": 3, "b": 5, "d": 4}

# printing dictionary
print("Before clearing...")
print(data)

# clearing dictionary
data.clear()

# printing dictionary after clear()
print("After clearing...")
print(data)

Output

Before clearing...
{'a': 1, 'c': 3, 'b': 5, 'd': 4}
After clearing...
{}

Comments and Discussions!

Load comments ↻


Advertisement
Advertisement



Copyright © 2024 www.includehelp.com. All rights reserved.