Python List del Function

Python del function is used to delete an item from a list at a user-specified index. The index position of the List del function starts from 0 and ends at n-1 and the syntax of this is:

del Name[Index_Position]

Python List del Function Example

The Python list del function removes the value at a specified index. After deleting, the remaining values moved up to fill the index gap. If we know the Index value or want to eliminate a range of items from the given object, then use del statement to remove the element. The following code removes elements at index positions 5, 7, 0, and 3.

number = [9, 4, 17, -2, 10, -17, 18, 55, 1, 90]
 
print("Original Elements are  : ", number)
 
del number[5]
print("After Deleting Element at Index 5 : ", number)
 
del number[7]
print("After Deleting Element at Index 7 : ", number)
 
del number[0]
print("After Deleting Element at Index 0 : ", number)
 
del number[3]
print("After Deleting Element at Index 3 : ", number)
Python List del Function 1

Python List delete Function Example 2

In this example, we declared a string. Next, we used this del function to eliminate string words at index position 4, 2, and 0.

TIP: Please refer to the List and functions in Python.

fruits = ['Banana', 'Apple', 'Grape', 'Orange', 'Kiwi', 'Cherry']
 
print("Items are       : ", fruits)
 
del fruits[4]
print("After Removing Item at Index 4 : ", fruits) 

del fruits[2]
print("After Removing Item at Index 2 : ", fruits)
 
del fruits[0]
print("After Removing Item at Index 0 : ", fruits)
Items are       :  ['Banana', 'Apple', 'Grape', 'Orange', 'Kiwi', 'Cherry']
After Removing Item at Index 4 :  ['Banana', 'Apple', 'Grape', 'Orange', 'Cherry']
After Removing Item at Index 2 :  ['Banana', 'Apple', 'Orange', 'Cherry']
After Removing Item at Index 0 :  ['Apple', 'Orange', 'Cherry']

In this Python list del function example,

  1. We are deleting an item at index position 1, which is Orange. After deleting the Orange, the index position of Grape automatically become one and Banana as 2.
  2. We remove an item at position 1, which is Grape.
  3. Within the Fourth, we removed the non-existing item. That is why it throws an error saying index out of range.
Fruits = ['Apple', 'Orange', 'Grape', 'Banana']
del Fruits[1]
print(Fruits)
print('========')

del Fruits[1]
print(Fruits)
print('========')

del Fruits[1]
print(Fruits)
print('========')

del Fruits[1]
print(Fruits)
['Apple', 'Grape', 'Banana']
========
['Apple', 'Banana']
========
['Apple']
========
Traceback (most recent call last):
  File "/Users/suresh/Desktop/simple.py", line 14, in <module>
    .... Fruits[1]
IndexError: assignment index out of range

Let me use the delete function on Mixed elements (String, Number, Negative Numbers).

fruits = ['Banana', 2, 'Grape', 9, -3, 'Orange', -1, 'Kiwi', 7]
 
print("Items are       : ", fruits)
 
del fruits[5]
print("After Removing Item at 5 : ", fruits)
 
del fruits[6]
print("After Removing Item at 6 : ", fruits)
 
del fruits[2]
print("After Removing Item at 2 : ", fruits) 
Items are       :  ['Banana', 2, 'Grape', 9, -3, 'Orange', -1, 'Kiwi', 7]
After Removing Item at 5 :  ['Banana', 2, 'Grape', 9, -3, -1, 'Kiwi', 7]
After Removing Item at 6 :  ['Banana', 2, 'Grape', 9, -3, -1, 7]
After Removing Item at 2 :  ['Banana', 2, 9, -3, -1, 7]

Python List delete Example 4

Here, we declared a Nested and this program deletes the Nested items at index positions 1, 3, and 5.

values = [[10, 22], [11, 3], [15, 25], [45, 65], [75, 98, 225]]
 
print("Items are       : ", values)
 
del values[1]
print("Items after Eleminating at 1 : ", values)
 
del values[3]
print("Items after Eleminating at 3 : ", values)
 
del values[5]
print("Items after Eleminating at 5 : ", values)
Items are       :  [[10, 22], [11, 3], [15, 25], [45, 65], [75, 98, 225]]
Items after Eleminating at 1 :  [[10, 22], [15, 25], [45, 65], [75, 98, 225]]
Items after Eleminating at 3 :  [[10, 22], [15, 25], [45, 65]]
Traceback (most recent call last):
  File "/Users/suresh/Desktop/simple.py", line 11, in <module>
   .... number[5]
IndexError: assignment index out of range