Use app×
Join Bloom Tuition
One on One Online Tuition
JEE MAIN 2025 Foundation Course
NEET 2025 Foundation Course
CLASS 12 FOUNDATION COURSE
CLASS 10 FOUNDATION COURSE
CLASS 9 FOUNDATION COURSE
CLASS 8 FOUNDATION COURSE
0 votes
218 views
in Computer by (51.9k points)
closed by

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.

1 Answer

+1 vote
by (49.5k points)
selected by
 
Best answer
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]

Welcome to Sarthaks eConnect: A unique platform where students can interact with teachers/experts/students to get solutions to their queries. Students (upto class 10+2) preparing for All Government Exams, CBSE Board Exam, ICSE Board Exam, State Board Exam, JEE (Mains+Advance) and NEET can ask questions from any subject and get quick answers by subject teachers/ experts/mentors/students.

Categories

...