Menu Close

Python List clear() Method

Python list clear method

In this Python list method tutorial, You will learn about the Python list clear() method. clear in Python is used to clear or empty the list. In Python, clear is a built-in function.
In the previous Python list method tutorial, we have seen all about the list append() method.

Python list clear() method:

In Python, the list clear() method is a built-in method that is used to clear the elements of the list or clean the list.

Syntax of Python list clear method:

list.clear()

Return Value.

The return value of the clear() function in Python is None.

Python List Clear() Method Parameter:

In Python, the list clear function does not accept any parameter.

Example 1:

Here, we will clear the list of the fruits using the list clear() function.

fruits = ['apple', 'banana', 'cherry']
fruits.clear()
print(fruits)

Output

[]

Example 1:

fruits = ['apple', 'banana', 'cherry']
car = {
  "brand": "Ford",
  "model": "Mustang",
  "year": 1964
}

fruits.append(car)
print(fruits)
fruits.clear()
print(fruits)

Output

['apple', 'banana', 'cherry', {'brand': 'Ford', 'model': 'Mustang', 'year': 1964}]
[]

See Also:

Conclusion

In this tutorial, you have learned about the Python list clear method. clear function in Python allows us to clear or clean all the items on the list. Python list clear function is a built-in function.

If you like this Python list append method tutorial, Please share comments and share with your friends who want to learn Python programming.

For More Reference:- Click Here

Python List extend() method

Related Posts