List operation: Removing an item

Miguel E. miguest66REMOVE at REMOVEyahoo.com
Sun Apr 16 00:39:37 EDT 2006


Hi,

I've been (self) studying Python for the past two months and I have had
no background in OOP whatsoever.

I was able to write an interactive program that randomly selects an item
 from a list. From a Main Menu, the user has the option to add items to
an empty list, show the list, run the random selector, and of course
quit the program. The program also complains if a user tries to add an
item that is already in the list prompting the user to enter another item.

I am trying to create a function that removes an item as specified by
the user. Apparently, the list operation "del list[:]" deletes the
entire list. Below is the sample function.

Any ideas, suggestions, or tips on the list operation I should be using,
or a better way of writing this?

TIA



--------------------------------------------

shopList = ['eggs', 'milk', 'bread', 'juice', 'fruit', 'deli']

def removeItem():
   "Remove an item from the list"
   print
   for items in shopList:
      print items
   print
   dlete = raw_input("Enter the item you want to remove: ")
   print
   for item in shopList:
      if dlete in shopList:
         del shopList[:]  # <--What is the correct function to use?
         print "%s has been removed" % dlete
         print "The new list is now", shopList
      else:
         print "Item is not in the list"



More information about the Python-list mailing list