In the previous article, we have discussed Python List append() Method with Examples
List in Python:
Lists in Python are mutable sequences. They are extremely similar to tuples, except they do not have immutability constraints. Lists are often used to store collections of homogeneous things, but there is nothing stopping you from storing collections of heterogeneous items as well.
List clear() Method in Python:
The clear() method clears or removes all the elements from the list.
Syntax:
list.clear()
Parameter Values: This method has no parameters.
Return Value:
The clear() method only removes items from the specified list. It doesn’t return anything.
Examples:
Example1:
Input:
Given list = ["good", "morning", "btechgeeks"]
Output:
The given list is : ['good', 'morning', 'btechgeeks'] The given list after applying clear() method: []
Example2:
Input:
Given list = [25, 35, 67, 12, 15]
Output:
The given list is : [25, 35, 67, 12, 15] The given list after applying clear() method: []
List clear() Method with Examples in Python
Method #1: Using Built-in Functions (Static Input)
Approach:
- Give the list as static input and store it in a variable.
- Print the above-given list.
- Applying the clear() method to the given list that clears or removes all the elements from the given list.
- Print the given list after removing all its elements.
- The Exit of Program.
Below is the implementation:
# Give the list as static input and store it in a variable. gvn_lst = ["good", "morning", "btechgeeks"] # Print the above-given list. print("The given list is :", gvn_lst) # Applying the clear() method to the given list that clears or removes all the # elements from the given list. gvn_lst.clear() # Print the given list after removing all its elements. print("The given list after applying clear() method:", gvn_lst)
Output:
The given list is : ['good', 'morning', 'btechgeeks'] The given list after applying clear() method: []
Method #2: Using Built-in Functions (User Input)
Approach:
- Give the list as user input using list(),map(),input(),and split() functions.
- Store it in a variable.
- Print the above-given list.
- Applying the clear() method to the given list that clears or removes all the elements from the given list.
- Print the given list after removing all its elements.
- The Exit of Program.
Below is the implementation:
# Give the list as user input using list(),map(),input(),and split() functions. # Store it in a variable. gvn_lst = list(map(int, input( 'Enter some random List Elements separated by spaces = ').split())) # Print the above-given list. print("The given list is :", gvn_lst) # Applying the clear() method to the given list that clears or removes all the # elements from the given list. gvn_lst.clear() # Print the given list after removing all its elements. print("The given list after applying clear() method:", gvn_lst)
Output:
Enter some random List Elements separated by spaces = 2 4 6 9 1 The given list is : [2, 4, 6, 9, 1] The given list after applying clear() method: []
Note: You cannot use the clear() method if you are using Python 2 or Python 3.2 or lower. Instead, use the del operator.
Using del to clear the list
Approach:
- Give the list as static input and store it in a variable.
- Print the above-given list.
- Clear all the elements of the given list using the del operator.
- Print the given list after removing all its elements.
- The Exit of Program.
Below is the implementation:
# Give the list as static input and store it in a variable. gvn_lst = [50, 100, 150, 200] # Print the above-given list. print("The given list is :", gvn_lst) # Clear all the elements of the given list using the del operator. del gvn_lst[:] # Print the given list after removing all its elements. print("The given list after applying del operator:", gvn_lst)
Output:
The given list is : [50, 100, 150, 200] The given list after applying del operator: []
Know about the syntax and usage of functions to manipulate lists with the provided Python List Method Examples and use them.