List operation: Removing an item

M

Miguel E.

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"
 
D

Dennis Lee Bieber

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.
Shouldn't be a surprise... any [x:y] specifies a slice of a list.
The defaults for "x" and "y" are "beginning of list" and "end of list"
respectively.

You only want to specify the single item...
shopList = ['eggs', 'milk', 'bread', 'juice', 'fruit', 'deli']
if itm in shopList:
.... del shopList[shopList.index(itm)]
.... else:
.... print "Not available"
....
print shopList ['eggs', 'milk', 'bread', 'fruit', 'deli']
if itm in shopList:
.... del shopList[shopList.index(itm)]
.... else:
.... print "Not available"
....
Not available
 
S

Steven D'Aprano

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.

If you know the value of the item, and not its position:

somelist.remove("value")

This will raise an exception if "value" is not in somelist.

If you know the item's position:

del somelist[position]

or

somelist[position:position+1] = []

or even:

somelist = somelist[:position] + somelist[position+1:]

(That last example is quite inefficient for lists, but it is a useful
technique to remember for immutable sequences like strings.)
 
T

Terry Reedy

Miguel E. said:
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.

Do you really need the items sorted? I strongly suspect that a set would
work better. Much easier to randomly add, delete, and check membership.

Terry Jan Reedy
 
F

Fulvio

Do you really need the items sorted?

Do you really think he isn't a troll?

See the latest thread on lists end related PEP.....
However there's always some good info on your reply (all of those had reply to
OP).

F
 
S

Steven D'Aprano

Do you really need the items sorted? I strongly suspect that a set would
work better. Much easier to randomly add, delete, and check membership.

The OP didn't say anything about having the items sorted.

I think he's trying to learn how to use Python, as he says, and
specifically learn about lists at this moment. Telling him to use sets
isn't going to help him learn about lists.
 
S

Steven D'Aprano

Do you really think he isn't a troll?

[scratches head]

The OP looks like a beginner trying to experiment with Python as a way of
learning how to program in it. What makes you think he's a troll?
 
T

Terry Reedy

Steven D'Aprano said:
The OP didn't say anything about having the items sorted.

Which, along with the specification of unique members, is why I questioned
the wisdom of using a list which is sorted and does allow duplicates.
I think he's trying to learn how to use Python, as he says, and
specifically learn about lists at this moment. Telling him to use sets
isn't going to help him learn about lists.

Learning about how to use any particular type includes learning when to use
it and just as importantly, when not to use it. The OP's difficulties seem
to me to stem from not using the right type. It would have been a
disservice to him for nobody to mention that and steer him in a better
direction.

Terry Jan Reedy
 
M

Miguel E

Steven said:
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.


If you know the value of the item, and not its position:

somelist.remove("value")

This will raise an exception if "value" is not in somelist.

Thank you. That fixed it.

Regards,



-M
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,731
Messages
2,569,432
Members
44,832
Latest member
GlennSmall

Latest Threads

Top