L ists 199 15 25 35 45 55 id of list after function

This preview shows page 11 - 14 out of 18 pages.

LISTS199[15, 25, 35, 45, 55]ID of list after function call: 65565640The list after the function call:[10, 20, 30, 40, 50]9.8 LISTMANIPULATIONIn this chapter, we have learnt to create a list and thedifferent ways to manipulate lists. In the followingprograms, we will apply the various list manipulationmethods.Program 9-3 Write a menu driven program to performvarious list operations, such as:Append an elementInsert an elementAppend a list to the given listModify an existing elementDelete an existing element from its positionDelete an existing element with a given valueSort the list in ascending orderSort the list in descending orderDisplay the list.#Program 9-3#Menu driven program to do various list operationsmyList = [22,4,16,38,13] #myList already has 5 elementschoice = 0while True:print("The list 'myList' has the following elements", myList)print("\nL I S TO P E R A T I O N S")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")print(" 10. Exit")choice = int(input("ENTER YOUR CHOICE (1-10): "))#append elementif choice == 1:element = int(input("Enter the element to be appended: "))myList.append(element)Ch 9.indd19908-Apr-1912:40:13 PM
COMPUTERSCIENCE– CLASSXI200print("The element has been appended\n")#insert an element at desired positionelif choice == 2:element = int(input("Enter the element to be inserted: "))pos = int(input("Enter the position:"))myList.insert(pos,element)print("The element has been inserted\n")#append a list to the given listelif choice == 3:newList = int(input("Enter the list to be appended: "))myList.extend(newList)print("The list has been appended\n")#modify an existing elementelif choice == 4:i = int(input("Enter the position of the element to bemodified: "))if i < len(myList):newElement = int(input("Enter the new element: "))oldElement = myList[i]myList[i] = newElementprint("The element",oldElement,"has been modified\n")else:print("Position of the element is more than the lengthof list")#delete an existing element by positionelif choice == 5:i = int(input("Enter the position of the element to bedeleted: "))if i < len(myList):element = myList.pop(i)print("The element",element,"has been deleted\n")else:print("\nPosition of the element is more than the lengthof list")#delete an existing element by valueelif choice == 6: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 orderCh 9.indd20008-Apr-1912:40:13 PM
LISTS201elif choice == 7:myList.sort()print("\nThe list has been sorted")#list in reverse sorted orderelif choice == 8:myList.sort(reverse = True)print("\nThe list has been sorted in reverse order")#display the listelif choice == 9:print("\nThe list is:", myList)#exit from the menuelif choice == 10:breakelse:

Upload your study docs or become a

Course Hero member to access this document

Upload your study docs or become a

Course Hero member to access this document

End of preview. Want to read all 18 pages?

Upload your study docs or become a

Course Hero member to access this document

Term
Fall
Professor
NoProfessor
Tags
Method, Chemical element

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture