Menu-Driven Python Program to Perform List Operations
In Python, a list is a built-in data structure used to store multiple items in a single variable. A list can contain numbers, strings, or items of different data types. Lists are mutable, which means their elements can be added, modified, or removed after the list is created.
Python provides several built-in list methods, such as append(), insert(), extend(), remove(), pop(), sort(), and reverse(), to perform different operations on a list.
In this article, we will develop a menu-driven Python program to perform various list operations. The user will select an operation from a menu, and the program will perform the selected task.
Objectives of the Program
After studying this program, students will be able to:
- Create and display a list in Python.
- Add one or more elements to a list.
- Insert an element at a specified position.
- Remove elements from a list.
- Search for an element and count its occurrences.
- Sort and reverse a list.
- Use a menu-driven approach with conditional statements and loops.
Python Program to Perform List Operations
The following program provides a menu for performing common list operations. It uses a while loop to display the menu repeatedly until the user selects the Exit option.
# Menu-driven program to perform list operations
my_list = []
while True:
print("\n----- LIST OPERATIONS MENU -----")
print("1. Create a list")
print("2. Display the list")
print("3. Append an element")
print("4. Insert an element")
print("5. Extend the list")
print("6. Remove an element")
print("7. Pop an element")
print("8. Delete an element by index")
print("9. Count occurrences")
print("10. Find index of an element")
print("11. Sort the list")
print("12. Reverse the list")
print("13. Find length of the list")
print("14. Find maximum element")
print("15. Find minimum element")
print("16. Clear the list")
print("17. Exit")
try:
choice = int(input("Enter your choice (1-17): "))
except ValueError:
print("Invalid input! Please enter a number.")
continue
if choice == 1:
elements = input(
"Enter list elements separated by spaces: "
).split()
my_list = elements
print("List created successfully.")
elif choice == 2:
print("List:", my_list)
elif choice == 3:
element = input("Enter the element to append: ")
my_list.append(element)
print("Updated list:", my_list)
elif choice == 4:
element = input("Enter the element to insert: ")
try:
index = int(input("Enter the index: "))
if 0 <= index <= len(my_list):
my_list.insert(index, element)
print("Updated list:", my_list)
else:
print("Invalid index.")
except ValueError:
print("Please enter a valid integer index.")
elif choice == 5:
elements = input(
"Enter elements separated by spaces: "
).split()
my_list.extend(elements)
print("Updated list:", my_list)
elif choice == 6:
element = input("Enter the element to remove: ")
if element in my_list:
my_list.remove(element)
print("Updated list:", my_list)
else:
print("Element not found.")
elif choice == 7:
if my_list:
print("Removed element:", my_list.pop())
print("Updated list:", my_list)
else:
print("The list is empty.")
elif choice == 8:
try:
index = int(input("Enter the index to delete: "))
if -len(my_list) <= index < len(my_list):
del my_list[index]
print("Updated list:", my_list)
else:
print("Invalid index.")
except ValueError:
print("Please enter a valid integer index.")
elif choice == 9:
element = input("Enter the element to count: ")
print("Occurrences:", my_list.count(element))
elif choice == 10:
element = input("Enter the element to search: ")
if element in my_list:
print("Index:", my_list.index(element))
else:
print("Element not found.")
elif choice == 11:
my_list.sort()
print("Sorted list:", my_list)
elif choice == 12:
my_list.reverse()
print("Reversed list:", my_list)
elif choice == 13:
print("Length of the list:", len(my_list))
elif choice == 14:
if my_list:
try:
print("Maximum element:", max(my_list))
except TypeError:
print("Elements of different types cannot be compared.")
else:
print("The list is empty.")
elif choice == 15:
if my_list:
try:
print("Minimum element:", min(my_list))
except TypeError:
print("Elements of different types cannot be compared.")
else:
print("The list is empty.")
elif choice == 16:
my_list.clear()
print("List cleared successfully.")
elif choice == 17:
print("Exiting the program. Goodbye!")
break
else:
print("Invalid choice! Please select from 1 to 17.")
ch=’y’
L = [1,2,3,4,2,3,2,4,5,6,71,1,10,12]
while ch == ‘y’ or ch==’Y’:
print(“1. append in list”)
print(“2. Insert in a list”)
print(“3. Find length of list”)
print(“4. To count occurence of an element”)
print(“5. Extend a list”)
print(“6. Find the sum of list elements”)
print(“7. Find the index of given element”)
print(“8. Find the minimum element in a list”)
print(“9. Find the maximum element in a list”)
print(“10.To pop element”)
print(“11.To remove element”)
print(“12.To use del”)
print(“13.To reverse a list”)
print(“14.To sort a list”)
x = int(input(“Enter choice = “))
if x == 1:
print(“Original list “, L)
val = int(input(“Enter a value to be appended -> “))
L.append(val)
print(“List after append “, L)
if x == 2:
val = int(input(“Enter a value to be inserted -> “))
loc = int(input(“Enter a location to insert -> “))
L.insert(loc,val)
print(“Original list “, L)
print(“List after insert”,L)
if x == 3:
n = len(L)
print(“Length of list “,n)
if x == 4:
val = int(input(“Enter a value whose occurence want to count -> “))
n = L.count(val)
print(val,” occur “,n,”times in list”, L)
if x == 5:
M = [100,200,300,4000]
L.extend(M)
print(“List after extend “,L)
if x == 6:
n = sum(L)
print(“Sum of list elements “,n)
if x == 7:
print(L)
print(“1. To give index when no stariing and ending value given”)
print(“2. To give index after given staring index value”)
print(“3. To give index after given staring and ending index value”)
y = int(input(“Enter choice -> “))
if y == 1:
val = int(input(“Enter a value whose index you want -> “))
n = L.index(val)
print(“index of “, val,” is “, n)
if y == 2:
val = int(input(“Enter a value whose index you want -> “))
loc = int(input(“Enter a staring location -> “))
n = L.index(val,loc)
print(“index of “, val,” is “, n)
if y == 3:
val = int(input(“Enter a value whose index you want -> “))
loc = int(input(“Enter a staring location -> “))
end = int(input(“Enter a ending location -> “))
n = L.index(val,loc,end)
print(“index of “, val,” is “, n)
if x == 8:
n = min(L)
print(“Minimum element of list “,n)
if x == 9:
n = max(L)
print(” maximum element of list “,n)
if x == 10:
print(“1. To pop last element “)
print(“2. To pop a particular index”)
y = int(input(“Enter the choice”))
if y == 1:
print(“List before pop “,L)
n = L.pop()
print(“List after pop “,L)
if y == 2:
loc = int(input(“Enter a location to pop -> “))
print(“List before pop “,L)
n = L.pop(loc)
print(“List after pop “,L)
if x == 11:
print(“List before remove “,L)
val = int(input(“Enter a value want to remove -> “))
L.remove(val)
print(“List after remove “,L)
if x == 12:
print(“1. To delete complete list”)
print(“2. To delete a particular element from list”)
y = int(input(“Enter the choice”))
if y == 1:
print(“List before del “,L)
del L
print(“List after del “,L)
if y == 2:
print(“List before pop “,L)
loc = int(input(“Enter a location of element to delete -> “))
del L[loc]
print(“List after del “,L)
if x == 13:
print(“List before reverse”,L)
L.reverse()
print(“List after reverse “,L)
if x == 14:
print(“List before sort”,L)
L.sort()
print(“List in ascending order “,L)
L.sort(reverse=True)
print(“List in descending order “,L)
ch = input(“\n Do you want to continue Y/N = “)
