clear() in Python | clear() Method in Python

Learn via video course
FREE
View all courses
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Topics Covered

Overview

The clear() function in Python removes all elements from a list. The function does not take any parameters and does not return anything.

Syntax of clear() function in Python

Transform Your Career

Choose from our industry-leading programs designed for career success

NSDC Certified

Modern Software and AI Engineering Program

Master full-stack development with AI integration

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Modern Data Science and ML with specialisation in AI

Advanced data science techniques with AI specialization

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Advanced AIML with Specialisation in Agentic AI

Deep dive into AIML with focus on Agentic systems

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

DevOps, Cloud & AI Platform Engineering

Build and manage AI-powered cloud infrastructure

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

AI Engineering Advanced Certification by IIT-Roorkee

Premier AI engineering certification from IIT-Roorkee

3 MonthsDuration
AI-LedCurriculum
Career SupportSupport
Program highlights
Go to Program

Parameters of clear() function in Python

The clear method in Python doesn’t take any parameters.

Return Type of clear() function in Python

The clear() function simply clears the given list. It does not return any particular value.

Scaler Placement Report and Statistics

₹23L
AVG CTC
SCALER PLACEMENT PROOF

Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.

11,000+placements
650+companies
Verified data

Example of clear() function in Python

Code:

Output:

What is clear() List method in Python?

The clear() method clears a list of all its entries. It removes everything from the list and returns an empty list. It doesn't take any parameters and doesn't throw an exception if the list is empty.

What is clear list in Python

Note:

The clear() function is part of the standard library and entirely clears the Python list.

Turn Learning into Career Growth

1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary
1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary

More Examples

Example 1: Emptying the list using del() method as an alternative of clear() method

Code:

Output:

Explanation:

In the above example code snippet, we have made use of del to clear a list in Python. First, we have declared a list. Next, we have used del and then printed the list to see its contents. Similar to clear, del also empties the list.

Example 2: Using the clear() method if the list is already empty

Output:

Explanation:

Here, we have taken an example of a list that is already empty. On iterating the list after using the clear function, we can see that no elements are stored in the list.

Scaler Placement Report and Statistics

₹23L
AVG CTC
SCALER PLACEMENT PROOF

Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.

11,000+placements
650+companies
Verified data

Conclusion

  • The clear() method of the standard library is used while working with lists and dictionaries.
  • It does not take any inputs or parameters.
  • The function does not return anything but clears the list's contents or dictionary.

See Also

Hiring Partners:
GoogleGoogleAmazonAmazonMicrosoftMicrosoftFlipkartFlipkartAdobeAdobe1200+ more