a) Program:
def deleteElements():
global list1
#Asking the user if he want to delete any element from the list
inp = input("Do you want to delete any element from the list? (Y/N) ")
#if user input is yes
if(inp == 'Y' or inp == 'y'):
elem = int(input("Enter the element which you would like to delete: "))
#Using remove() function to remove the element
for a in list1:
if(a == elem):
list1.remove(elem)
print("The element is deleted from the list. ")
deleteElements()
else:
print("The elements in the list",list1)
#Defining empty list
list1 = []
#Taking the number of elements to be added as input
inp = int(input("How many elements do you want to add in the list? "))
#Taking the input from user
for i in range(inp):
a = int(input("Enter the elements: "))
list1.append(a)
#Printing the list
print("The list entered is:",list1)
#The function delete element is called
deleteElements()
OUTPUT:
How many elements do you want to add in the list? 10
Enter the elements: 1
Enter the elements: 2
Enter the elements: 9
Enter the elements: 8
Enter the elements: 4
Enter the elements: 5
Enter the elements: 7
Enter the elements: 11
Enter the elements: 13
Enter the elements: 15
The list entered is: [1, 2, 9, 8, 4, 5, 7, 11, 13, 15]
Do you want to delete any element from the list? (Y/N) Y
Enter the element which you would like to delete: 11
The element is deleted from the list.
Do you want to delete any element from the list? (Y/N) n
The elements in the list [1, 2, 9, 8, 4, 5, 7, 13, 15]
b) Program:
def deleteElementsAtIndex():
global list1
#Asking the user if he want to delete any element from the list
inp = input("Do you want to delete any element from the list? (Y/N) ")
#if user input is yes
if(inp == 'Y' or inp == 'y'):
index = int(input("Enter the index of the element you would like to delete: "))
#Using pop() function to remove the element at desired index
if(index < len(list1)):
list1.pop(index)
print("The element is deleted from the list. ")
else:
print("The index is out of range.")
deleteElementsAtIndex()
else:
print("The elements in the list",list1)
#Defining empty list
list1 = list()
#Taking the number of elements to be added as input
inp = int(input("How many elements do you want to add in the list? "))
#Taking the input from user
for i in range(inp):
a = int(input("Enter the elements: "))
list1.append(a)
#Printing the list
print("The list entered is:",list1)
#The function deleteElementsAtIndex is called to delete the element after taking user input
deleteElementsAtIndex()
OUTPUT:
How many elements do you want to add in the list? 7
Enter the elements: 1
Enter the elements: 2
Enter the elements: 3
Enter the elements: 7
Enter the elements: 6
Enter the elements: 5
Enter the elements: 4
The list entered is: [1, 2, 3, 7, 6, 5, 4]
Do you want to delete any element from the list? (Y/N) y
Enter the index of the element you would like to delete: 3
The element is deleted from the list.
Do you want to delete any element from the list? (Y/N) n
The elements in the list [1, 2, 3, 6, 5, 4]