banner

© 2004-21 ict4u.net

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
listFruit.pop()
print ('Last Item  removed: {}\n'.format(listFruit))

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

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