Register Login

How to remove element from an Array in Python

Updated Aug 04, 2021

Arrays are one of the most common forms of data structure available in almost all programming languages. They are fast and easy to use because of their indexing feature. They linearly store data, and hence they come as an example of linear data structure. In this article, you will learn about the concept of Arrays and how to remove elements from arrays.

What are arrays?

Arrays in Python are data structures similar to that of Lists. They are used as objects and can store data of different types. This built-in data structure allows us to store and prepare data so that it can be managed properly by utilizing the various predefined methods available. Arrays are beneficial and efficient if you are planning to work on a lot of values that are of the same data type. Python arrays store homogenous values only. Let us now try the different ways through which you can remove elements from a Python Array.

To create an array, you have to use the array() method. This method takes two parameters. The first parameter is the data type code while the second is the collection of linear data of same type.

Syntax:

array.array('d', [2.2, 4.4, 6.6])

The data type code table is written here

‘b’ signed char int 1
‘B’ unsigned char int 1
‘l’ signed long int 4
‘L’ unsigned long int 4
‘f’ float float 4
‘d’ double float 8
‘h’ signed short int 2
‘H’ unsigned short int 2
‘u’ Py_UNICODE Unicode  2
‘i' signed int int 2
‘I’ unsigned int int 2

Removing an element from an Array in Python:

There are various ways of removing a particular element from an array. Let us take a look at each of them:

Method 1: Using the del keyword:

The del keyword is used for removing the entire object from the memory location as well as delete any specific element from the collection object through its index value.

import array as arry
ar = arry.array('d', [3.14, 6.22, 4.79])
print('Orignal array: ', ar)
del ar[1]
print('After removing element:', ar)

Output:

Orignal array:  array('d', [3.14, 6.22, 4.79])
After removing element: array('d', [3.14, 4.79])

Explanation:

Here, we have to import the array module. Next, we have to create a homogenous linear data structure using the array() and we have stored it in the ‘ar’. Then we have printed the ar to see all the values within it before deleting. Then, we use the indexing method to delete the second element of the array by referring to index value 1. Now, when we will print the array again, we will see that the element in the second position is missing.

Method 2: Using the pop() method:

For removing an element from an array, developers can also use the pop() method of the array. If we simply use pop() without passing any parameter, then it will remove the element from the last (n th) index. But if we specify the index value of the array, it will remove that particular element from the array.

Syntax:

array_object.pop(optional_parameter)

Program 1:

import array as ar
data = ar.array('i', [26, 64, 38, 43])
print('Orignal array: ',data)
data.pop()
print('Array after removing element: ', data)

Output:

Orignal array:  array('i', [26, 64, 38, 43])
Array after removing element:  array('i', [26, 64, 38])

Explanation:

Here, we have to import the array module. Next, we have to create a homogenous linear data structure using the array() and we have stored it in the ‘data’. Now, we have used the pop() that will remove the element from the end or from the last (index value n-1) of the array. It is because we have not passed any position value as an argument. Finally, we have printed the data structure to see the changes.

Program 2:

import array as ar
data = ar.array('i', [26, 64, 38, 43])
print('Orignal array: ',data)

data.pop(2)
print('Array after removing element: ', data)

Output:

Orignal array:  array('i', [26, 64, 38, 43])
Array after removing element:  array('i', [26, 64, 43])

Explanation:

Here, we have to import the array module. Next, we have to create a homogenous linear data structure using the array() and we have stored it in the ‘data’. This time we used the pop() method with an argument (here we passed the 2nd position so that it gets popped out). Finally, we have printed the data structure to see the changes.

Method 3: using the remove() method:

The remove method is another predefined method of the array module that is used to remove a specific element from the array. In this case, you have to specify the particular value that one wants to remove.

Syntax:

array_object.remove(specific_element_name)

Program:

import array as ar
data = ar.array('i', [26, 64, 38, 43])
print('Orignal array: ',data)

data.remove(26)
print('Array after removing element: ', data)

Output:

Orignal array:  array('i', [26, 64, 38, 43])
Array after removing element:  array('i', [64, 38, 43])

Explanation:

Here, we have to import the array module. Next, we have to create a homogenous linear data structure using the array() and we have stored it in the ‘data’. Here we have used to remove() method that will remove the specified element mentioned in the parameter. The remove() method does not takes any index location as value, instead it takes the actual value that is residing in the array. Finally, we have printed the array to see the changes.

Method 4: Using setdiscard() method:

The setdiscard() method is another useful method for removing elements from the array. Its removal is done to the point and hence, does not demand extra space. But, for using it, we have to convert the array object into a set object using the set() function.

Program:

import array as ar
data = ar.array('i', [26, 64, 38, 43])
s = set(data)
print('Orignal array:', s)

# Remove element by value
s.discard(26)
print('Array aftr removing element: ', s)

Output:

Orignal array: {64, 26, 43, 38}
Array aftr removing element:  {64, 43, 38}

Explanation:

Here, we have to import the array module. Next, we have to create a homogenous linear data structure using the array() and we have stored it in the ‘data’. Then, we use the set() to typecast it explicitly and stored it in a new object called ‘s’. Then we use the discard() method of the set to remove that particular element specified as the argument. Finally, we have printed the array to see the changes.

Method 5: Using the Lambda function along with the filter() nesting each other:

The Lambda function is an essential concept of Python that allows us to make it work as a utility tool to perform the tough task in just a single line. Although Lambda and filter functions are one-liners, using these functions performs an overwrite of data and requires extra space. The filter() will filter out all the mentioned elements and will put them in a new array (iterative object). Also, it removes all the occurrences of elements.

Program:

import array as ar
test = ar.array('i', [26, 64, 38, 43])
# Displaying the initial array data
print ("The entire array before removal of elements is : " + str(test))
test = list(filter(lambda x: x != 26, test))
# Displaying the array data after removal of specific element
print ("The entire array after removal of elements is : " + str(test))

Output:

The entire array before removal of elements is : array('i', [26, 64, 38, 43])
The entire array after removal of elements is : [64, 38, 43]

Explanation:

Here, we have to import the array module. Next, we have to create a homogenous linear data structure using the array() and we have stored it in the ‘test’. Now, we have displayed the initial array data. Then we use the list() and nesting the filter() method that will extract out the selective data picked up by the lambda() from the test array. Once the picked element is taken out using the not-equals operator, it will display the array data after removal of specific element.

Method 6: Using List comprehension:

List Comprehensions are also one-liner codes, and also removes all the occurrences of elements. It works similar to that of the lambda function and requires extra space, and overwrites the data. It is slightly faster than Lambda and filter() because, in list comprehension you do not have to use the nesting of functions.

Program:

import array as ar
test = ar.array('i', [26, 64, 38, 43])
print ("Displaying the complete array before removing any element: " + str(test))
test2 = [g for g in test if g != 64]
print ("Displaying the complete array after removing an element: " + str(test2))

Output:

The entire array before removal of elements is : array('i', [26, 64, 38, 43])
The entire array after removal of elements is : [64, 38, 43]

Explanation:

Here, we have to import the array module. Next, we have to create a homogenous linear data structure using the array() and we have stored it in the ‘test’. Now, we have displayed the initial array data in string format. Then we use the list comprehension to extract the data we want to remove using the loop that will iterate every element of the array since it is an iterable object of Python.

Using the not equals operator, we picked that particular element that we don’t want further in our array test and initialized all other elements in a fresh new object test2. Finally, we display the test2’s data using print().

Conclusion:

All of these techniques can help in removing elements from an array. But, among these, using the del statement is the most efficient one as it uses a single keyword to remove an element specified with the index value. The other two efficient ways are through the use of pop() and remove() methods of the array. The setdiscard() is not that efficient because we need to convert our array into set in order to perform this task.
The lambda function with filter() is the least efficient because it not only uses nesting of methods to perform the task but also performs overwrite and takes extra space in removing the element. List comprehension is also not efficient as it uses a self-loop in a single line. And, as we all know using loops increases time complexity. Also, it requires extra space, and performs overwrites which makes it slower than the top three techniques.


×