ByteBoard Notes Notes

Perform list operations using a menu: append, insert, remove, pop, sort, reverse, count and index

Informatics Practices XI · Experiment 18 of 36 · Python

11-IPPython programs
Expt. No.18
Page No.36

Menu-driven list operations

Aim

To write a menu-driven Python program that performs the list methods append, insert, remove, pop, sort, reverse, count and index on a list.

Concepts used

Algorithm

  1. Start with a list of numbers.
  2. Show a menu of operations and input a choice; repeat until Exit.
  3. Call the matching list method and display the list after it.

Program

# Menu-driven list operations lst = [30, 10, 50, 20]while True:    print("List:", lst)    print("1.append 2.insert 3.remove 4.pop 5.sort 6.reverse 7.count 8.index 9.exit")    ch = int(input("Choice: "))    if ch == 1:        lst.append(int(input("Value: ")))    elif ch == 2:        pos = int(input("Position: "))        lst.insert(pos, int(input("Value: ")))    elif ch == 3:        lst.remove(int(input("Value to remove: ")))    elif ch == 4:        print("Popped:", lst.pop())    elif ch == 5:        lst.sort()    elif ch == 6:        lst.reverse()    elif ch == 7:        v = int(input("Value: "))        print(v, "occurs", lst.count(v), "time(s)")    elif ch == 8:        v = int(input("Value: "))        print(v, "is at index", lst.index(v))    elif ch == 9:        break

A marked line continues the line above it. In your file, write it on the same line.

Output

$ python list_operations_menu.py
List: [30, 10, 50, 20] 1.append 2.insert 3.remove 4.pop 5.sort 6.reverse 7.count 8.index 9.exit Choice: 1 Value: 40 List: [30, 10, 50, 20, 40] 1.append 2.insert 3.remove 4.pop 5.sort 6.reverse 7.count 8.index 9.exit Choice: 2 Position: 0 Value: 5 List: [5, 30, 10, 50, 20, 40] 1.append 2.insert 3.remove 4.pop 5.sort 6.reverse 7.count 8.index 9.exit Choice: 3 Value to remove: 50 List: [5, 30, 10, 20, 40] 1.append 2.insert 3.remove 4.pop 5.sort 6.reverse 7.count 8.index 9.exit Choice: 5 List: [5, 10, 20, 30, 40] 1.append 2.insert 3.remove 4.pop 5.sort 6.reverse 7.count 8.index 9.exit Choice: 6 List: [40, 30, 20, 10, 5] 1.append 2.insert 3.remove 4.pop 5.sort 6.reverse 7.count 8.index 9.exit Choice: 7 Value: 10 10 occurs 1 time(s) List: [40, 30, 20, 10, 5] 1.append 2.insert 3.remove 4.pop 5.sort 6.reverse 7.count 8.index 9.exit Choice: 8 Value: 20 20 is at index 2 List: [40, 30, 20, 10, 5] 1.append 2.insert 3.remove 4.pop 5.sort 6.reverse 7.count 8.index 9.exit Choice: 4 Popped: 5 List: [40, 30, 20, 10] 1.append 2.insert 3.remove 4.pop 5.sort 6.reverse 7.count 8.index 9.exit Choice: 9

✓ Printed by running this program, not typed by hand.

Result

Each choice changed the list as expected: 40 was appended, 5 inserted at index 0, 50 removed, the list sorted and then reversed, count and index found 10 and 20, and pop() removed the last element.

Viva voce: what the examiner may ask

01What is the difference between remove() and pop()?

remove(x) deletes the first element equal to x; pop() deletes by position (the last one by default) and returns it.

02What is the difference between append() and insert()?

append() always adds at the end; insert(i, x) adds x at index i.

03What happens if remove() is given a value that is not in the list?

Python raises ValueError.

Variations for the lab test

  1. Add an option to extend the list with another list.
  2. Check that a value is in the list before removing it.
  3. Add an option to display the sum, max and min.
! Common mistake Calling lst.index(99) for a value that is not in the list stops the program with ValueError.
Back to the 11-IP index