codescracker
python

Python Program to Delete Element from a List



« Python Examples Python Tutorial »


In this article, I've created some programs in Python, that deletes or removes an element (or multiple elements) from a list. Both element and list must be entered by user at run-time. Here are the list of programs:

Delete an Element from List by Value

This program deletes an element (entered by user) from a list (also entered by user) by value. The question is, write a Python program to delete an element from list. Here is its answer:

# ----codescracker.com----

print("Enter 10 Elements: ")
arr = []
for i in range(10):
    arr.append(input())

print("\nEnter the Value to Delete: ")
val = input()

arr.remove(val)
print(arr)

Here is its sample run:

python delete element from list by value

Now supply the input say 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 as ten elements, and a value say 4 to delete it from the list and print the new list as shown in the snapshot given below:

delete element from list by value python

Note - The append() method appends an element at the end of a list.

Note - The remove() method removes an element (by value provided as its argument) from a list.

In above program, the following code:

for i in range(10):

is used to execute the following statement:

arr.append(input())

ten times with value of i from 0 to 9. Therefore, all ten numbers entered by user as provided in the sample run, gets stored in the list arr[] in this way:

arr = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']

Now using the following statement:

arr.remove(val)

the value stored in val variable gets removed from the list arr. For example, if value of val is 4, then 4 gets deleted from the list named arr.

Delete Element from List of Given Size

This is the modified version of previous program with an extra feature added to it. For example this program allows user to define the size of list. The end used in this program, to skip inserting an automatic newline using print()

# ----codescracker.com----

print(end="Enter the Size of List: ")
tot = int(input())

arr = []
print(end="Enter " +str(tot)+ " Elements: ")
for i in range(tot):
    arr.append(input())

print(end="\nEnter the Value to Delete: ")
val = input()

if val in arr:
    arr.remove(val)
    print("\nThe New list is: ")
    for i in range(tot-1):
        print(end=arr[i]+" ")
else:
    print("\nElement doesn't exist in the List!")

Here is its sample run with user input, 5 as size of list, and 1, 2, 3, 4, 5 as five elements of list, then 4 as element to delete:

python delete element from list

Here is another sample run with user input 12 as size of list and c, o, d, e, s, c, r, a, c, k, e, r as twelve elements of list, then c as element to delete from this list:

delete an element from list python

Note - As you can see from this sample run, the first occurrence of c gets deleted from the list. But what if user wants to delete the c at second occurrence, then we've to follow the program that deletes an element from list by index as given below.

Note - The str() method converts any type of value to a string type.

Delete an Element from List by Index

The pop() method is used to delete an element from list by index number. Whereas the remove() method is used to delete an element by its value. Let's have a look at the program given below:

# ----codescracker.com----

print(end="Enter the Size of List: ")
tot = int(input())

arr = []
print(end="Enter " +str(tot)+ " Elements: ")
for i in range(tot):
    arr.append(input())

print(end="\nEnter the Index Number: ")
index = int(input())

if index<tot:
    arr.pop(index)
    print("\nThe New list is: ")
    for i in range(tot-1):
        print(end=arr[i]+" ")
else:
    print("\nInvalid Index Number!")
print()

Here is its sample run with user input, 12 as size and c, o, d, e, s, c, r, a, c, k, e, r as twelve elements of list. Now to delete second c from the list, enter 5 as index number. Because second 'c' is available at fifth index. Indexing starts with 0.

delete element from list by index python

The dry run of above program with same user input as provided in sample run of this program, goes like:

Delete an Element with all Occurrences from List

Now this program deletes all occurrences of an element by value. For example, if user enters 1, 2, 3, 2, 4, 2 as six elements, and 2 as element to delete, then all three 2s gets deleted from the list:

# ----codescracker.com----

print(end="Enter the Size of List: ")
tot = int(input())

arr = []
print(end="Enter " +str(tot)+ " Elements: ")
for i in range(tot):
    arr.append(input())

print(end="\nEnter an Element to Delete with all Occurrences: ")
num = input()

if num in arr:
    while num in arr:
        arr.remove(num)

    print("\nThe New list is: ")
    tot = len(arr)
    for i in range(tot):
        print(end=arr[i] + " ")
else:
    print("\nElement doesn't Found in the List!")
print()

Here is its sample run with user input 6 as size and 1, 2, 3, 2, 4, 2 as six elements, and 2 as element to delete with all its occurrences:

delete all occurrences from list python

In above program, the following block of code:

while num in arr:
    arr.remove(num)

states that, the statement inside the while, that is arr.remove(num) gets executed until the condition num in arr evaluates to be false. The num in arr code checks whether the value of num available in the list named arr or not. If available, then condition evaluates to be true, otherwise evaluates to be false.

Delete Multiple Elements from List

This program allows user to delete multiple elements from list at one execution. For example, if given elements of list are 1, 2, 3, 4, 5, 6, 7, 8 and if user wants to delete two elements from the list, then enter 2 as number of element to delete, and then two values say 3 and 6 to delete these two elements as shown in the program and its output given below:

# ----codescracker.com----

print(end="Enter the Size of List: ")
tot = int(input())

arr = []
print(end="Enter " +str(tot)+ " Elements: ")
for i in range(tot):
    arr.append(input())

print(end="How many Elements to Delete ? ")
noOfElement = int(input())

print(end="Enter " +str(noOfElement)+ " Elements to Delete: ")

delList = []
for i in range(noOfElement):
    delList.append(input())

for i in range(noOfElement):
    if delList[i] in arr:
        while delList[i] in arr:
            arr.remove(delList[i])

print("\nThe New list is: ")
tot = len(arr)
for i in range(tot):
    print(end=arr[i] + " ")
print()

Here is its sample run with following user inputs:

Let's have a look at the sample run given below:

delete multiple elements from list python

Note - The len() returns length of thing (list) passed as its argument

In above program, I've created a list named delList, that stores list of elements (entered by user) that has to be delete from the list named arr (also entered by user). That is using indexing of delList list, I've checked whether the current element available in the original list named arr or not. If available, then delete, otherwise continue to check for next, until the last element of delList


« Python Examples Python Tutorial »