Q. Write a program to read elements of a list.

a)The program should ask for the position of the element to be deleted from the list. Write a function to delete the element at the desired position in the list.

b)The program should ask for the value of the element to be deleted from the list.  Write a function  to delete the element of this value from the list.


Answer :-


def deletepos( pos ):
    lst.pop(pos)
    return lst

def deleteelem( elem ):
    lst.remove ( elem )
    return lst

lst = eval(input("Enter a list :-"))
print( "Enter your choise :-" )
print("1. to delete by position ")
print("2. to delete by element ")
choise = input("Enter (1 / 2)")
if choise == "1" :
    position = int(input("Enter the position :-"))
    print("New List :-", deletepos( position ))

elif choise == "2" :
    element = int(input("Enter element :-"))
    print("New List :-", deleteelem( element ))

Post a Comment

You can help us by Clicking on ads. ^_^
Please do not send spam comment : )

Previous Post Next Post