Page 449 - Computer Science V2.0 Class 11
P. 449

print(f"Element at position {position} deleted. Updated list:", myList)
                          else:
                              print("Invalid position. Please enter a valid position.")

                      def deleteElementByValue(myList, value):
                          if value in myList:
                              myList.remove(value)
                              print(f"Element {value} deleted. Updated list:", myList)
                          else:
                              print(f"Element {value} not found in the list.")

                      myList = []

                      n = int(input("Enter the number of elements in the list: "))
                      for i in range(n):
                          element = int(input(f"Enter element {i + 1}: "))
                          myList.append(element)

                      print("\nOriginal List:", myList)

                      choice = input("Do you want to delete by position (P) or by value (V)? ").upper()

                      if choice == "P":
                          position = int(input("Enter the position of the element to delete: "))
                          deleteElementByPosition(myList, position)
                      elif choice == "V":
                          value = int(input("Enter the value of the element to delete: "))
                          deleteElementByValue(myList, value)
                      else:
                          print("Invalid choice. Please enter 'P' or 'V'."
                    9.  Read a list of n elements. Pass this list to a function which reverses this list in-place without creating a new list.
                  Ans.   def reverseInPlace(inputList):
                          start, end = 0, len(inputList) - 1

                          while start < end:
                              inputList[start], inputList[end] = inputList[end], inputList[start]
                              start += 1
                              end -= 1

                      n = int(input("Enter the number of elements: "))

                      myList = []
                      for i in range(n):
                          element = int(input(f"Enter element {i + 1}: "))
                          myList.append(element)

                      print("\nOriginal List:", myList)

                      reverseInPlace(myList)

                      print("Reversed List in Place:", myList)


                                                                                                        Dictionaries  435
   444   445   446   447   448   449   450   451   452   453   454