Page 109 - TCS ICT Book 8
P. 109

The City School  2021-2022




             Delete Elements in List

             Del command is used to delete a list element as mentioned in the example below:

             Sample code:

             # This defines a list called newList
             newList=[10,20,30,”Samsung”]

             # Deletes the element at the index 1
             del newList[1]

             # Prints the list after deleting an item
             print(newList)

            Output:                                                                                                 Programming Robots & Single-Board Computers

            [10, 30, ‘Samsung’]

             Add Elements in List

             To add an item to the end of the list, use the append() method. The code below
             demonstrates this:


             Sample code:


             # This defines a list called thisList
             thisList=[”apple”,”banana”,”cherry”]

             # adds a new element at the end of the list
             thisList.append(“orange“)

             # Prints the list after deleting an item
             print(thisList)


             Output:
             [‘apple’,‘banana’,‘cherry’,‘orange’]






























                                                                                                                109
   104   105   106   107   108   109   110   111   112   113   114