© 2004-21

Removing data

There are a number of ways to remove data elements from a list
#Author: A Knifton
# date:
# Purpose: demonstrate ways to remove data from a list

print ("{} Removing Data from a List {} \n".format("-"*6,"-"*6))

# set up the initial contents of the list
listFruit = ["Orange", "Apple", "Pear", "Mango", "Grapefruit", "Lemon", "Kiwi", "Plum", "Peach", "Passion Fruit"]
print ('Initial List: {}\n'.format(listFruit))

#remove the last item in the list
print ('Last Item  removed: {}\n'.format(listFruit))

#remove an item by selecting the data
print ('Item {} removed: {}\n'.format("Grapefruit",listFruit))

# remove an item by popping its id:
idFruit = listFruit.index("Apple")
print ('Fruit Apple, id:{} pop(ed): {}\n'.format(idFruit,listFruit))