Skip to content
Programs · Program 9-3

Q.Program 9-3: Write a menu driven program to perform various list operations, such as:
• 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 ascending order
• Sort the list in descending order
• Display the list.

Uttarakhand UbseTextbookSubjective· 5mImportance★★★★★est
23% · 6/26 Questions
🔒 Locked · start free trial →

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 →

Wrap the nine list operations in functions, show a numbered menu inside a while True loop, read the user's choice and dispatch with if/elif — append(), insert(), extend(), index assignment, pop(), remove(), sort(), sort(reverse=True) and print() cover every menu item.

The idea (list operations behind a menu). Every operation the menu asks for is a single built-in list capability. The program's job is orchestration: keep looping, read a choice, validate inputs (positions in range, values present), perform the operation on ONE list that lives for the whole session, and let the user display or exit at will.

Program

def display(lst):
    print("The list is:", lst)

def main():
    lst = []
    while True:
        print()
        print("========= LIST OPERATIONS MENU =========")
        print("1.  Append an element")
        print("2.  Insert an element at a position")
        print("3.  Append a list to the given list")
        print("4.  Modify an existing element")
        print("5.  Delete an element from its position")
        print("6.  Delete an element with a given value")
        print("7.  Sort the list in ascending order")
        print("8.  Sort the list in descending order")
        print("9.  Display the list")
        print("10. Exit")
        choice = int(input("Enter your choice (1-10): "))

        if choice == 1:
            lst.append(int(input("Enter the element to append: ")))
        elif choice == 2:
            pos = int(input("Enter the position (0-based): "))
            if 0 <= pos <= len(lst):
                lst.insert(pos, int(input("Enter the element: ")))
            else:
                print("Invalid position")
        elif choice == 3:
            items = input("Enter elements separated by spaces: ").split()
            lst.extend([int(x) for x in items])
        elif choice == 4:
            pos = int(input("Enter the position to modify: "))
            if 0 <= pos < len(lst):
                lst[pos] = int(input("Enter the new value: "))
            else:
                print("Invalid position")
        elif choice == 5:
            pos = int(input("Enter the position to delete: "))
            if 0 <= pos < len(lst):
                print("Deleted:", lst.pop(pos))
            else:
                print("Invalid position")
        elif choice == 6:
            val = int(input("Enter the value to delete: "))
            if val in lst:
                lst.remove(val)
            else:
                print(val, "is not in the list")
        elif choice == 7:
            lst.sort()
            print("Sorted in ascending order")
        elif choice == 8:
            lst.sort(reverse=True)
            print("Sorted in descending order")
        elif choice == 9:
            display(lst)
        elif choice == 10:
            print("Bye!")
            break
        else:
            print("Invalid choice, try again")

main()

Sample run (expected output, abridged to the interaction lines)

Enter your choice (1-10): 1
Enter the element to append: 30
Enter your choice (1-10): 1 …

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.