Remove an element from a list by index in Python

Python List

In this post, we are going to learn to Remove an element from a list by index in Python. Python list supports methods to delete an element based on its index or value. So let us learn these methods one by one with the help of examples.

These are Python list built-in methods that use to remove an element from the list.

  • Using List.Remove() Method
  • Using List.pop() Method
  • Using del keyword Method

Similarly, we can remove multiple elements from the list by index with the help of the below functions.

  • del slicing
  • indices

1.List.Remove() to delete element by value


The List. remove() method is an in-built method. It deletes the first occurrence of the given element from the list by value. We pass the value of the element we want to delete as an argument.

Syntax

list.remove(value)

Parameters

  • value : is required parameter represent the element we want to delete

Program Example

mylist = ['C#','lang','Go','Data','C#','16','17','35','68']

#removing the element by value

mylist.remove('C#')

print('list after removing element = \n',mylist)

Output

list after removing element = 
 ['lang', 'Go', 'Data', 'C#', '16', '17', '35', '68']

1.2 try/catch block ValueError

Note: Whenever we try to remove a value from the list that does not exist then the remove() method throws a ValueError: list.remove(x): x not in the list,

Program Example

mylist = ['C#','lang','Go','Data','C#','16','17','35','68']

#removing the element by value

if 'Python' in mylist:
  mylist.remove('Python')  
else:
  print('given element is not exist in the list')

#Output: given element do not exist in the list

Therefore it is advisable to check first if the value exists or not. Otherwise use the try/catch block to add error handling for this case as shown below.

Program Using try/catch block

mylist = ['C#','lang','Go','Data','C#','16','17','35','68']
try:
  mylist.remove('Python')  
except ValueError:
  print(' given element is not exist in the list')

#Output: given element do not exist in the list

2. List.Pop() Method to delete element by index


The Python List pop() is the in-built list method that removes the element based on the index and returns the deleted element. If the argument is not passed to the pop() method the last element is deleted. The index starts at 0 or we can use negative indexing -1 to access the last element or the number of element-1.

Note: Trying removing a nonexistence index, throws error as IndexError: pop index out of range

Program Example : Delete Element by index

mylist = ['C#','lang','Go','Data','C#','16','17','35','68']

mylist.pop(8)  

print('after removing element: \n',mylist)

Output

after removing element: 
 ['C#', 'lang', 'Go', 'Data', 'C#', '16', '17', '35']

3.List del keyword to Remove element by index


The Python List del keyword is used to remove an element from the list, we use it followed by a list name and pass the index to the list in []. The index starts at 0 and the last index would be list length or -1 to access the last element.

Syntax

del list[index]

Note: If we call del list[index] on non-existence index then it generates runtime IndexError error.

Program Example

mylist = ['C#','lang','Go','Data','C#','16','17','35','68']

del mylist[2]

print('after removing element: \n',mylist)

Output

after removing element: 
 ['C#', 'lang', 'Data', 'C#', '16', '17', '35', '68']

4. Remove elements from list by index range


As we just learned above that remove(), pop() is used to remove an element. Sometimes we have to remove multiple elements at once. To do this job we use the del keyword with slicing. We pass the start/stop index of elements from the list to del. The elements those fall in this range will be removed.

Syntax

del list[start:stop]

Program Example

mylist = ['C#','lang','Go','Data','C#','16','17','35','68']
#removing range of element
del mylist[1:3]
print('after removing mutiple element: \n',mylist)

Output

mylist = ['C#','lang','Go','Data','C#','16','17','35','68']
del mylist[1:3]
print('after removing mutiple element: \n',mylist)

Specifying Step with del keyword slicing

We can specify the step size with the del keyword to remove the elements. This is the way to pass the step size as [start:stop: step]

Program Example

mylist = ['C#','lang','Go','Data','C#','16','17','35','68']
del mylist[1:3:1]
print('after removing mutiple element: \n',mylist)

Output

after removing mutiple element: 
 ['C#', 'Data', 'C#', '16', '17', '35', '68']

5.Remove multiple elements from a list by indices


Here, in this code, we are removing elements from the list at indexes(3,5,6).

mylist = ['C#','lang','Go','Data','C#','16','17','35','68']
# remove the element at indexs 3,5,6 
item_remove_indexes = [3, 5, 6]

#function to remove at mutiple indexs
def remove_mul_ele(input_list,list_of_indexes):
    indexes = sorted(list_of_indexes, reverse=True)
    for id in indexes :
        if id < len(input_list):
            input_list.pop(id)
    print('list after remove multiple items by index =\n',input_list)           
#calling the remove_multiple_element
remove_mul_ele(mylist, item_remove_indexes)

Output

list after remove multiple items index  =
 ['C#', 'lang', 'Go', 'C#', '35', '68']

Conclusion: We hope after going through Remove items from the list by index in Python, you will be able to remove elements from the list using these methods.