LIST OPERATIONS- PROJECT FOR BEGINNERS
# -*- coding: utf-8 -*-
"""
Created on Fri Aug 16 10:18:23 2024
@author: Kirti Hora
"""
myList = [] #myList having 5 elements
choice = 0
print("\nL I S T O P E R A T I O N S")
ans='y'
while ans=='y':
print(" 1. Append an element")
print(" 2. Insert an element at the desired position")
print(" 3. Append a list to the given list")
print(" 4. Modify an existing element")
print(" 5. Delete an existing element by its position")
print(" 6. Delete an existing element by its value")
print(" 7. Sort the list in ascending order")
print(" 8. Sort the list in descending order")
print(" 9. Display the list")
choice = int(input("ENTER YOUR CHOICE (1-9): "))
#append element
if choice == 1:
#WAP to append an element in a list
n =int (input("Enter no of elements"))
i=0
while i < n:
num= int(input("Enter element " + str(i) + ": "))
myList+=[num]
i+=1
print("The elements have been appended\n")
#insert an element at desired position
elif choice == 2:
#WAP to insert element at a particular position
element = eval(input("Enter the element to be inserted: "))
pos = int(input("Enter the position:"))
myList.insert(pos,element)
print("The element has been inserted\n")
elif choice == 3:
# WAP to enter list in the existing list
newList = eval(input("Enter the list to be appended: "))
myList.extend(newList)
print("The list has been appended\n")
#modify an existing element
elif choice == 4:
#WAP to to modify the element in the list
i = int(input("Enter the position of the element to be modified: "))
if i < len(myList):
newElement = eval(input("Enter the new element: "))
oldElement = myList[i]
myList[i] = newElement
print("The element",oldElement,"has been modified\n")
else:
print("Position of the element is more then the length of list")
#delete an existing element by position
elif choice == 5:
#WAP to select the element on particular position
i = int(input("Enter the position of the element to be deleted: "))
if i < len(myList):
element = myList.pop(i)
print("The element",element,"has been deleted\n")
else:
print("\nPosition of the element is more then the length of list")
#delete an existing element by value
elif choice == 6:
# WAP to remove particular element from the list
element = int(input("\nEnter the element to be deleted: "))
if element in myList:
myList.remove(element)
print("\nThe element",element,"has been deleted\n")
else:
print("\nElement",element,"is not present in the list")
#list in sorted order
elif choice == 7:
# WAP to sort the given list
myList.sort()
print("\nThe list has been sorted")
#list in reverse sorted order
elif choice == 8:
# WAP a to print list in reverse order
myList.sort(reverse = True)
print("\nThe list has been sorted in reverse order")
#display the list
elif choice == 9:
print("\nThe list is:", myList)
else:
print("Choice is not valid")
ans=input("Do you want to continue..")
else:
print("Terminating")
Comments
Post a Comment