Decode School

Decode School - The School for Problem Solvers

Welcome to my blog. Subscribe and get my latest blog post in your inbox.

delete element in list python

Python Program to get an element and delete the element from list.

Sample Input 1:

5 5 7 9 3 1 9

Sample Output 1:

5 7 3 1

Program or Solution

					    
							
l=list(map(int,input("Enter Numbers:").split()))
e=int(input("Enter the element to delete"))
for i in range(0,len(l)):
    if(l[i]==e):
        for j in range(i,len(l)-1):
            l[j]=l[j+1]
        l[j+1]=None
print(l)
            

					    
					

Program Explanation

visit every location in the list if current element in list l[i] is equal to the element which is to be deleted.

Then move all the elements located after i to its previous position using: l[i] = l[i+1]