List operation: Removing an item

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Miguel E.

    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("Ente r 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"
  • Steven D'Aprano

    #2
    Re: List operation: Removing an item

    On Sat, 15 Apr 2006 22:39:37 -0600, Miguel E. wrote:
    [color=blue]
    > 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.[/color]

    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:positi on+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.

    Comment

    • Terry Reedy

      #3
      Re: List operation: Removing an item


      "Miguel E." <miguest66REMOV E@REMOVEyahoo.c om> wrote in message
      news:1243ikb79q bujfc@corp.supe rnews.com...[color=blue]
      > 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.[/color]

      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



      Comment

      • Fulvio

        #4
        Re: List operation: Removing an item

        On Sunday 16 April 2006 13:37, Terry Reedy wrote:[color=blue]
        >Do you really need the items sorted?[/color]

        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

        Comment

        • Steven D'Aprano

          #5
          Re: List operation: Removing an item

          On Sun, 16 Apr 2006 01:37:44 -0400, Terry Reedy wrote:
          [color=blue]
          >
          > "Miguel E." <miguest66REMOV E@REMOVEyahoo.c om> wrote in message
          > news:1243ikb79q bujfc@corp.supe rnews.com...[color=green]
          >> 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.[/color]
          >
          > 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.[/color]

          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.

          Comment

          • Steven D'Aprano

            #6
            Re: List operation: Removing an item

            On Sun, 16 Apr 2006 19:05:50 +0800, Fulvio wrote:
            [color=blue]
            > On Sunday 16 April 2006 13:37, Terry Reedy wrote:[color=green]
            >>Do you really need the items sorted?[/color]
            >
            > Do you really think he isn't a troll?[/color]

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

            Comment

            • Terry Reedy

              #7
              Re: List operation: Removing an item


              "Steven D'Aprano" <steve@REMOVETH IScyber.com.au> wrote in message
              news:pan.2006.0 4.16.13.31.34.6 83427@REMOVETHI Scyber.com.au.. .[color=blue]
              > On Sun, 16 Apr 2006 01:37:44 -0400, Terry Reedy wrote:[color=green]
              >> 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.[/color]
              >
              > The OP didn't say anything about having the items sorted.[/color]

              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.
              [color=blue]
              > 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.[/color]

              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



              Comment

              • Miguel E

                #8
                Re: List operation: Removing an item

                Steven D'Aprano wrote:[color=blue]
                > On Sat, 15 Apr 2006 22:39:37 -0600, Miguel E. wrote:
                >
                >[color=green]
                >>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.[/color]
                >
                >
                > 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.
                >[/color]

                Thank you. That fixed it.

                Regards,



                -M

                Comment

                Working...