Python Program to delete element from an array(list)

In this tutorial, we will learn writing python program to delete a given element from an array (list in case of python) and print the array(list).

Problem Statement

Our problem statement is, we have to delete a given element from an array (list) using python program.

For example:

Case 1: if the given array (list) is [1, 2, 3, 4] and the user gives input 3 to remove.

                The output should be [1, 2, 4].

Case 2: if the given array (list) is [9, 2, 4, 8] and the user gives input 4 to remove.

                 The output should be [9, 2, 8].

Our logic to delete a given element from an array(list)

  • Our program will take input to fix the size of the array (list).
  • For adding elements to the array (list), we use the ‘for loop’ and append() method.
  • Then, we take an element input from the user, which the user wants to remove from the array (list) created.
  • Print the new array (list) as the output after deleting the array.

Algorithm to delete a given element of an array(list)

Step 1: Start

Step 2: take an inputs from the user(let’s say size)

Step 3: create an empty list

Step 4: for i in range(0,size):

           elem=int(input(“Please give value for index “+str(i)+”: “))

           arr.append(elem)

Step 5: take the input element from the user that is needed to be removed.

Step 6:  arr.remove(num)

Step 7: print the array (list) ‘arr’.

Step 8: Stop

Python code to delete a given element of an array(list)

Program 1: Using the remove() Method

This is the most straightforward method for removing an element from a list in Python, provided the element exists in the list.

size=int(input("Enter the number of elements you want in array: "))
arr=[]
for i in range(0,size):
    elem=int(input("Please give value for index "+str(i)+": "))
    arr.append(elem)
num=int(input("Enter a number to remove from array : "))
arr.remove(num)
print("Array after removing",num,"=",arr)

Output:

Please give value for index 1: 43
Please give value for index 2: 2
Please give value for index 3: 55
Please give value for index 4: 12
Enter a number to remove from array : 12
Array after removing 12 = [22, 43, 2, 55]

Explanation:

For the input array (list) [22, 43, 2, 55, 12]. The user inputs element 12 to remove from the list and our program will execute the line arr.remove(12), which searches the 12 in the array (list) and then remove that element from the array(list).

Program 2: Using List Comprehension

List comprehension offers a way to create a new list by iterating over an existing one and including only those elements that do not match the given element.

size = int(input("Enter the number of elements you want in the array: "))
arr = []
for i in range(size):
    elem = int(input(f"Please give value for index {i}: "))
    arr.append(elem)
num = int(input("Enter a number to remove from the array: "))

new_numbers = [x for x in arr if x != num]

print("Array after removing:", new_numbers)

Output

Please give value for index 1: 2
Please give value for index 2: 1
Please give value for index 3: 3
Please give value for index 4: 4
Enter a number to remove from the array: 1
Array after removing: [2, 3, 4]

Explanation

This program uses list comprehension to build a new list that excludes all occurrences of the specified element. This method is useful when you need to remove all instances of the element, not just the first one.

Program 3: Using the pop() Method

If you know the index of the element you want to remove, you can use the pop() method, which removes the element at the specified position in the list.

size = int(input("Enter the number of elements you want in the array: "))
arr = []
for i in range(size):
    elem = int(input(f"Please give value for index {i}: "))
    arr.append(elem)
num = int(input("Enter a number to remove from the array: "))

new_array = list(filter(lambda x: x != num, arr))

print("Array after removing:", new_array)

Output

Enter the number of elements you want in the array: 6
Please give value for index 0: 1
Please give value for index 1: 3
Please give value for index 2: 5
Please give value for index 3: 2
Please give value for index 4: 6
Please give value for index 5: 1
Enter a number to remove from the array: 3
Array after removing: [1, 5, 2, 6, 1]

Explanation: This program uses filter() with a lambda function that checks if elements are not equal to the specified element. It’s particularly effective for removing all occurrences and can be useful in functional programming paradigms.