Python gives the following techniques to put off one or multiple elements. We can delete the factors the usage of the del key-word through specifying the index position. Let’s recognize the following methods.
remove()
pop()
clear()
del
List Comprehension – If specified condition is matched.
The remove() method
The remove() technique is used to put off the specified price from the list. It accepts the object fee as an argument. Let’s understand the following example.
Example –
list1 = ['Joseph', 'Bob', 'Charlie', 'Bob', 'Dave']
print("The list is: ", list1)
list1.remove('Joseph')
print("After removing element: ",list1)
Output:
The list is: ['Joseph', 'Bob', 'Charlie', 'Bob', 'Dave']
After removing element: ['Bob', 'Charlie', 'Bob', 'Dave']
If the listing includes greater than one item of the equal name, it eliminates that item’s first occurrence.
Example –
list1 = ['Joseph', 'Bob', 'Charlie', 'Bob', 'Dave']
print("The list is: ", list1)
list1.remove('Bob')
print("After removing element: ",list1)
Output:
The list is: ['Joseph', 'Bob', 'Charlie', 'Bob', 'Dave']
After removing element: ['Joseph', 'Charlie', 'Bob', 'Dave']
The pop() method
The pop() approach removes the item at the unique index position. If we have not unique the index position, then it gets rid of the ultimate object from the list. Let’s apprehend the following example.
Example –
list1 = ['Joseph', 'Bob', 'Charlie', 'Bob', 'Dave']
print("The list is: ", list1)
list1.pop(3)
print("After removing element: ",list1)
# index position is omitted
list1.pop()
print("After removing element: ",list1)
Output:
The list is: ['Joseph', 'Bob', 'Charlie', 'Bob', 'Dave']
After removing element: ['Joseph', 'Bob', 'Charlie', 'Dave']
After removing element: ['Joseph', 'Bob', 'Charlie']
We can additionally specify the negative index position. The index -1 represents the final object of the list.
Example –
list1 = ['Joseph', 'Bob', 'Charlie', 'Bob', 'Dave']
print("The list is: ", list1)
# Negative Indexing
list1.pop(-2)
print("After removing element: ",list1)
Output:
The list is: ['Joseph', 'Bob', 'Charlie', 'Bob', 'Dave']
After removing element: ['Joseph', 'Bob', 'Charlie', 'Dave']
The clear() method
The clear() method removes all gadgets from the list. It returns the empty list. Let’s apprehend the following example.
Example –
list1 = [10, 20, 30, 40, 50, 60]
print(list1)
# It will return the empty list
list1.clear()
print(list1)
Output:
[10, 20, 30, 40, 50, 60]
[]
The del statement
We can remove the listing object using the del keyword. It deletes the detailed index item. Let’s apprehend the following example.
Example –
list1 = [10, 20, 30, 40, 50, 60]
print(list1)
del list1[5]
print(list1)
del list1[-1]
print(list1)
Output:
[10, 20, 30, 40, 50, 60]
[10, 20, 30, 40, 50]
[10, 20, 30, 40]
It can delete the entire list.
del list1
print(list1)
Output:
Traceback (most recent call last):
File "C:/Users/DEVANSH SHARMA/PycharmProjects/Practice Python/first.py", line 14, in
print(list1)
NameError: name 'list1' is not defined
We can additionally delete the multiple objects from the list using the del with the slice operator. Let’s apprehend the following example.
Example –
list1 = [10, 20, 30, 40, 50, 60]
print(list1)
del list1[1:3]
print(list1)
del list1[-4:-1]
print(list1)
del list1[:]
print(list1)
Output:
[10, 20, 30, 40, 50, 60]
[10, 40, 50, 60]
[60]
[]
Using List Comprehension
The list comprehension is barely distinct way to get rid of the object from the list. It removes these items which fulfill the given condition. For instance – To cast off the even range from the given list, we define the condition as i % two which will give the reminder two and it will get rid of these objects that’s reminder is two
Let’s understand the following example.
Example –
list1 = [11, 20, 34, 40, 45, 60]
# Remove the odd numbers
print([i for i in list1 if i % 2 == 0])
#Remove the even numbers
print([i for i in list1 if i % 2 != 0])
Output:
[20, 34, 40, 60]
[11, 45]
Leave a Review