Python List del Function

Python List 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. The syntax of this Python List delete function is:

del List_Name[Index_Position]

Python List del Function Example 1

The following python code deletes List items at index positions 5, 7, 0, and 3.

# Python List del Function

number = [9, 4, 17, -2, 10, -17, 18, 55, 1, 90]

print("Original List Items are                  : ", number)

del number[5]
print("List Items after Deleting Item at Index 5 : ", number)

del number[7]
print("List Items after Deleting Item at Index 7 : ", number)

del number[0]
print("List Items after Deleting Item at Index 0 : ", number)

del number[3]
print("List Items after Deleting Item at Index 3 : ", number)
Python List del Function 1

Python del Function Example 2

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

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

# Python List del Function

fruits = ['Banana', 'Apple', 'Grape', 'Orange', 'Kiwi', 'Cherry']

print("List Items are       : ", fruits)

del fruits[4]
print("List after Deleting Item at Index 4 : ", fruits)

del fruits[2]
print("List after Deleting Item at Index 2 : ", fruits)

del fruits[0]
print("List after Deleting Item at Index 0 : ", fruits)

List del Function output

List Items are       :  ['Banana', 'Apple', 'Grape', 'Orange', 'Kiwi', 'Cherry']
List after Deleting Item at Index 4 :  ['Banana', 'Apple', 'Grape', 'Orange', 'Cherry']
List after Deleting Item at Index 2 :  ['Banana', 'Apple', 'Orange', 'Cherry']
List after Deleting Item at Index 0 :  ['Apple', 'Orange', 'Cherry']

List delete Example 3

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

# Python List del Function

fruits = ['Banana', 2, 'Grape', 9, -3, 'Orange', -1, 'Kiwi', 7]

print("List Items are       : ", fruits)

del fruits[5]
print("List after Deleting Item at Index 5 : ", fruits)

del fruits[6]
print("List after Deleting Item at Index 6 : ", fruits)

del fruits[2]
print("List after Deleting Item at Index 2 : ", fruits)

List del Function output

List Items are       :  ['Banana', 2, 'Grape', 9, -3, 'Orange', -1, 'Kiwi', 7]
List after Deleting Item at Index 5 :  ['Banana', 2, 'Grape', 9, -3, -1, 'Kiwi', 7]
List after Deleting Item at Index 6 :  ['Banana', 2, 'Grape', 9, -3, -1, 7]
List after Deleting Item at Index 2 :  ['Banana', 2, 9, -3, -1, 7]

Python List delete Example 4

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

# Python List del Function

number = [[10, 22], [11, 3], [15, 25], [45, 65], [75, 98, 225]]

print("List Items are       : ", number)

del number[1]
print("List Items after Deleting Item at Index 1 : ", number)

del number[3]
print("List Items after Deleting Item at Index 3 : ", number)

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