Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

python
clear
list
dictionary
communitycreator

What is the clear() method in Python?

Mahnoor Zaheer

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

The clear() method in Python empties a list or dictionary.

Syntax

The syntax of the clear() method is as follows.

objectName.clear()
The syntax of the clear() method in Python

Parameters and return value

This method neither takes any parameters nor does it return a value. clear simply removes all the elements from a list or dictionary.

Code

In the code below, we use the clear() method to empty the vowels list and contents dictionary.

# list
vowels = ['a','e','i','o','u']
# empty the list
vowels.clear()
# print the empty list
print('Empty Vowel list:', vowels)
# dictionary
contents = {'A': 1, 'B': 2, 'C': 3}
# empty the dictionary
contents.clear()
# print the empty list
print('Empty Contents Dictionary:', contents)
Code example of the clear() method in Python

RELATED TAGS

python
clear
list
dictionary
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring