473,473 Members | 1,974 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 2305
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
by: Thomas M. | last post by:
Hello, i have a question on the following code. test_list = for i in test_list: print i
3
by: Jeremy Owens-Boggs | last post by:
We are trying to implement a dual list box selection where you have two list boxes, You highlight items in the right side list box, click a button and this moves those items over to the left hand...
6
by: Jonathan | last post by:
Hi. I'm having trouble figuring out what I should be doing here. I'm trying to remove an object from a list. The function is: void Alive::FromRoom () { list<Alive>::iterator iter =...
3
by: Gregory Piñero | last post by:
I'm going to assume that it's supposed to work like this, but could someone tell me the reasoning behind it? I.E. why is 3 skipped? .... print item .... if item==2: .... alist.remove(item)...
16
by: pauldepstein | last post by:
Can anyone give a realistic example of a task for which the list container works better than vector or deque? Paul Epstein
10
by: pamelafluente | last post by:
Hi I have a sorted list with several thousands items. In my case, but this is not important, objects are stored only in Keys, Values are all Nothing. Several of the stored objects (might be a...
3
by: timmy | last post by:
i make a copy of a list, and delete an item from it and it deletes it from the orginal as well, what the hell is going on?!?!?! #create the staff copy of the roster Roster2 = for ShiftLine in...
1
by: mdpems | last post by:
Hi, I have a combo box that is set up to receive items from another from and post as a record set. The items will post to the list as I click an add item button. I also have a remove item button. I...
7
by: =?Utf-8?B?Sm9lbCBNZXJr?= | last post by:
I have created a custom class with both value type members and reference type members. I then have another custom class which inherits from a generic list of my first class. This custom listneeds...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.