Write a program to perform the below operations on the list:
- Create a list.
- Add/Remove an item to/from a list.
- Get the number of elements in the list.
- Access elements of the list using the index.
- Sort the list.
- Reverse the list.
Code
print("0. Print list")print("1. Create list")print("2. Add element to list")print("3. Delete element from list")print("4. Update element from list")print("5. Search element from list")print("6. Sort list")print("7. Reverse list")print("8. Exit")l = []while True:print("\nEnter your choice: ", end="")choice = int(input())if choice == 0:print(l)if choice == 1:print("Enter number of elements: ", end="")n = int(input())print("Enter elements")for i in range(n):l.append(input())print(l)elif choice == 2:print("Enter element to add: ", end="")l.append(input())print(l)elif choice == 3:print("Enter element to delete: ", end="")l.remove(input())print(l)elif choice == 4:print("Enter element to update: ", end="")l[l.index(input())] = input()print(l)elif choice == 5:print("Enter element to search: ", end="")print(l.index(input()))elif choice == 6:l.sort()print(l)elif choice == 7:l.reverse()print(l)elif choice == 8:break
Output
comment the output :)
0 Comments
Post a Comment