Programming Problems · Q7
Q.Write a program to read elements of a list and do the following.
a) The program should ask for the position of the element to be deleted from the list and 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 and delete this value from the list.
Odisha ChseTextbookSubjective· 3mImportance★★★★★est
90% · 27/30 Questions
You're viewing a preview — the full solution, concept, methods & PYQ mapping are locked.
Start your 14-day free trial to unlock the full solution →Two different delete tools for two different jobs: pop(index) removes the element at a
position (and returns it), while remove(value) searches for a value and deletes its
first occurrence. Validate before deleting — a bad index or a missing value raises an error.
Program
# Delete from a list (a) by position, (b) by value
n = int(input("How many elements? "))
lst = []
for i in range(n):
lst.append(int(input("Enter element " + str(i + 1) + ": ")))
print("List:", lst)
# a) delete by POSITION
pos = int(input("Position (index) to delete: "))
if 0 <= pos < len(lst):
removed = lst.pop(pos) # removes AND returns the element at that index
print("Deleted", removed, "-> list is now", lst)
else:
print("Invalid position!")
# b) delete by VALUE
val = int(input("Value to delete: "))
if val in lst:
lst.remove(val) # deletes the FIRST occurrence of the value
print("Deleted", val, "-> list is now", lst)
else:
print(val, "is not in the list!")
Sample run
How many elements? 5
Enter element 1: 11
Enter element 2: 22
Enter element 3: 33
Enter element 4: 44
Enter element 5: 55
List: [11, 22, 33, 44, 55]
Position (index) to delete: 2
Deleted 33 -> list is now [11, 22, 44, 55]
Value to delete: 44
Deleted 44 -> list is now [11, 22, 55]
Position vs value — the contrast that matters
| Task | Tool | Notes |
|---|---|---| …
Unlock everything free for 14 days
- Full step-by-step solutions
- Concept-first explanations
- Methods, shortcuts & mistakes
- PYQ mapping + timed mock tests
Full access for 14 days. No credit card required.