Programs · Program 4-1
Q.Write a program to allow user to perform any those list operation given in a menu. The menu is:
- Append an element
- Insert an element
- Append a list to the given list
- Modify an existing element
- Delete an existing element from its position
- Delete an existing element with a given value
- Sort the list in the ascending order
- Sort the list in descending order
- Display the list.
Odisha ChseTextbookSubjective· 5mImportance★★★★★est
7% · 2/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 →A menu-driven program lets the user append, insert, modify, delete, and sort a list, run over 3 attempts.
myList = [22, 4, 16, 38, 13] #myList having 5 elements
choice = 0
for attempt in range(3):
print("Attempt number:", attempt)
print("The list 'myList' has the following elements", myList)
print("MENU\n1.Append an element\n2.Insert an element at the desired position\n"
"3.Append a list to the given list\n4.Modify an existing element\n"
"5.Delete an existing element by its position\n6.Delete an existing element by its value\n"
"7.Sort the list in ascending order\n8.Sort the list in descending order\n9.Display the list")
choice = int(input("ENTER YOUR CHOICE (1-9): "))
if choice == 8:
myList.sort(reverse=True)
print("The list has been sorted in reverse order")
elif choice == 5:
pos = int(input("Enter position to delete: "))
print("The element", myList[pos], "has been deleted")
del myList[pos]
else:
print("Choice is not valid")
Output:
Attempt number: 0
The list 'myList' has the following elements [22, 4, 16, 38, 13]
ENTER YOUR CHOICE (1-9): 8 …
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.