469,929 Members | 1,742 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,929 developers. It's quick & easy.

List operation: Removing an item

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"
Apr 16 '06 #1
7 2188
On Sat, 15 Apr 2006 22:39:37 -0600, Miguel E. wrote:
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.)
--
Steven.

Apr 16 '06 #2

"Miguel E." <mi*************@REMOVEyahoo.com> wrote in message
news:12*************@corp.supernews.com...
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

Apr 16 '06 #3
On Sunday 16 April 2006 13:37, Terry Reedy wrote:
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
Apr 16 '06 #4
On Sun, 16 Apr 2006 01:37:44 -0400, Terry Reedy wrote:

"Miguel E." <mi*************@REMOVEyahoo.com> wrote in message
news:12*************@corp.supernews.com...
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.


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

Apr 16 '06 #5
On Sun, 16 Apr 2006 19:05:50 +0800, Fulvio wrote:
On Sunday 16 April 2006 13:37, Terry Reedy wrote:
Do you really need the items sorted?


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?
--
Steven.

Apr 16 '06 #6

"Steven D'Aprano" <st***@REMOVETHIScyber.com.au> wrote in message
news:pa****************************@REMOVETHIScybe r.com.au...
On Sun, 16 Apr 2006 01:37:44 -0400, Terry Reedy wrote:
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.


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

Apr 17 '06 #7
Steven D'Aprano wrote:
On Sat, 15 Apr 2006 22:39:37 -0600, Miguel E. wrote:

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
Apr 17 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

17 posts views Thread by Thomas M. | last post: by
3 posts views Thread by Jeremy Owens-Boggs | last post: by
3 posts views Thread by Gregory Piñero | last post: by
16 posts views Thread by pauldepstein | last post: by
10 posts views Thread by pamelafluente | last post: by
3 posts views Thread by timmy | last post: by
7 posts views Thread by =?Utf-8?B?Sm9lbCBNZXJr?= | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.