Skip to main content

CS / IP - LIST OPERATIONS - PROJECT - 16TH AUG 24

 

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

Popular posts from this blog

CS - SORTING/SEARCHING ALGORITHMS

  SORTING ALGORITHMS                       SORTING ALGORITHM PDF LINK #Bubble Sort          ·        The outer loop iterates through the entire array. ·        The inner loop compares adjacent elements and swaps them if they are out of order. ·        The outer loop runs n times, and each pass moves the largest element to its correct position. arr=[3,8,5,2,1] n = len(arr) print(n) for i in range(n):  #traverse through all the elements         # Last i elements are already sorted, no need to check them         for j in range(0, n-i-1):              # Swap if the element found is greater than the next element              if arr[j] > arr[j+1]:               ...

GRADE XI - NESTED FOR LOOP

                                                         NESTED FOR LOOP 1. for var1 in range(3):      print(var1,"OUTER LOOP")          # 0 outer loop       1 outer loop      2 outer loop          for var2 in range(2):                  print(var2+1,"INNER LOOP")    #1  2 inner loop     1  2  inner loop   1 2 inner loop  2. Print the following pattern using for loop: 1  1 2  1 2 3  1 2 3 4  Sol: for r in range(1,5):   #ROWS     for c in range(1,r+1):   #COLUMNS         print(c,end=" ")     print() 3. Print the following pattern using for loop: @  @ @  @ @ @...