In this post, we will understand How to remove element from array in Python. Python does not provide any specific data type as an array.Instead of array LIST is used as an array in Python also known as a dynamic array contains heterogenous sequence elements and each element store on specific index.We can also use NumPy array,Array module to create,add and delete elements perform operation on array.
1.How to remove element from array in Python
These are methods to delete or remove elements from the list.
- Del: The del statement is used to delete item by index,single, multiple elements, or a whole list.
- Pop() : It removes elements at specific Index.
- remove() : It is used to remove the passed element as a parameter.
- clear() : It clear() the list.
1. How to remove element from array in Python using del keyword
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 of list element list in []. The index starts at 0 and the last index would be list length or -1 to access the last element.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]
original_arr = ['at','on','up','in','upon']
# delete single element
del original_arr [1]
print('delete single element:',original_arr)
# delete multiple items from list using del
del original_arr [1:4]
print('delete range of element:',original_arr)
# delete the full array
del original_arr
Output
delete single element: ['at', 'up', 'in', 'upon']
delete range of element: ['at']
- How to Declare array in Python
- How to concatenate Array in Python
- How to initialize an array in Python
- How to extend NumPy array in Python
- How to add elements to array in Python
2. How to remove element from array in Python List.Pop()
The Python List pop() is in-built method 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
original_arr = ['at','on','up','in','upon']
# delete a single element at index 1
original_arr.pop(1)
print('after pop() :',original_arr)
Output
after pop() : ['at', 'up', 'in', 'upon']
3.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
original_arr = ['at','on','up','in','upon']
# delete a single element at index 1
original_arr .remove('at')
print('array after removing element = \n',original_arr)
Output
array after removing element =
['on', 'up', 'in', 'upon']
1.2 try/catch block ValueError with remove()
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
original_arr = ['at','on','up','in','upon']
original_arr .remove('at')
print('array after removing element = \n',original_arr)
#removing the element by value
if 'at' in original_arr:
original_arr.remove('at')
else:
print('given element is not exist in the array')
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.
Output
array after removing element =
['on', 'up', 'in', 'upon']
given element is not exist in the array
4. Remove element from array using list.clear()
To delete all the elements of array we can use the list.clear() method.It will clean the list and remove all the elements in the list.Let us understand with below examples.
# removing the 'upon' from the list
original_list.remove('upon')
print('after remove() :',original_list)
# clear the full list
original_list.clear()
print('after clear() :',original_list)
5. How to remove element from array in Python
In this example we have used NumPy libaray to create and delete element from array in Python.To use numpy libarya first we have to install on local system and import it in our program.
- import the libarya using import numpy as np
- The np.array() to create an array from given elemnet
- The np.delete() to delete element from numpy array.
import numpy as np
original_arr = np.array(['at','on','up','in','upon'])
resArr = np.delete(original_arr, [0, 1, 2])
print("array after delete element from index 0,1,2:\n", resArr)
Output
array after delete element from index 0,1,2:
['in' 'upon']