Table of Contents
In this tutorial, we will learn about the Python List clear() method with the help of examples.
The clear()
method removes all items from the list.
Example
prime_numbers = [2, 3, 5, 7, 9, 11] # remove all elements prime_numbers.clear() # Updated prime_numbers List print('List after clear():', prime_numbers) # Output: List after clear(): []
1. Syntax of List clear()
The syntax of clear()
method is:
list.clear()
2. clear() Parameters
The clear()
method doesn’t take any parameters.
3. Return Value from clear()
The clear()
method only empties the given list. It doesn’t return any value.
4. Example 1: Working of clear() method
# Defining a list list = [{1, 2}, ('a'), ['1.1', '2.2']] # clearing the list list.clear() print('List:', list)
Output
List: []
Note: If you are using Python 2 or Python 3.2 and below, you cannot use the clear()
method. You can use the del operator instead.
5. Example 2: Emptying the List Using del
# Defining a list list = [{1, 2}, ('a'), ['1.1', '2.2']] # clearing the list del list[:] print('List:', list)
Output
List: []
Visit this page to learn how del
operator works in Python.
Related posts:
Python Program to Check Leap Year
Python Set issubset()
Python staticmethod()
Python Program to Display Fibonacci Sequence Using Recursion
Python Program to Add Two Matrices
Python Program to Capitalize the First Character of a String
Python Program to Compute all the Permutation of the String
Python String capitalize()
Machine Learning with Python for everyone - Mark E.Fenner
Python datetime
Python bytes()
Python Set clear()
Python Program to Display Powers of 2 Using Anonymous Function
Python Program to Represent enum
Python Program to Count the Number of Digits Present In a Number
Python Closures
Python getattr()
Python Deeper Insights into Machine Learning - Sebastian Raschka & David Julian & John Hearty
Python Program to Shuffle Deck of Cards
Python String format_map()
Python reversed()
Python Program to Find the Factors of a Number
Partition a List in Java
Python String startswith()
Python int()
Python range()
Applied Text Analysis with Python - Benjamin Benfort & Rebecca Bibro & Tony Ojeda
Python List append()
Python Program to Check If Two Strings are Anagram
Python pass statement
Python Tuple count()
Python sum()