Problem with list.remove() method

Discussion in 'Python' started by Alvaro Combo, Nov 20, 2012.

  1. Alvaro Combo

    Alvaro Combo Guest

    Hi All,

    I'm relatively new to Python... but I have found something I cannot explain... and I'm sure you can help me.

    I have the following function that serves for removing the duplicates from a list... It's a simple and (almost) trivial task.

    I'm using WingIDE as editor/debugger and have Python 2.7.3.

    When running this I have an error when trying to remove cpy_lst[4]... and ONLY THAT!!! Even in interactive mode!!!

    Any suggestion is MOST welcome.

    Best Regards

    ACombo

    ....And the code:

    def remove_dup_5_10():
    """
    Remove the duplicates of a given list. The original list MUST be kept.
    """

    # Set the original list
    lst = ['a', 1, 10.0, 2, 'd', 'b', 'b', 'b', 1, 2, 'b' ]

    # NEED to create a copy... See dicussion on Problem 5.6 and issue #2
    cpy_lst = list(lst)

    # Perform an infinite loop... explained later
    i=0 # initialize the index
    while i != len(cpy_lst):
    if cpy_lst.count(cpy_lst) != 1:
    cpy_lst.remove(i)
    else:
    i += 1

    print "The original List: ", lst
    print "List with NO duplicates: ", cpy_lst

    return True
     
    Alvaro Combo, Nov 20, 2012
    #1
    1. Advertising

  2. On Wed, Nov 21, 2012 at 12:56 AM, Alvaro Combo <> wrote:
    > Hi All,
    >
    > I'm relatively new to Python... but I have found something I cannot explain... and I'm sure you can help me.
    >
    > I have the following function that serves for removing the duplicates from a list... It's a simple and (almost) trivial task.
    >
    > I'm using WingIDE as editor/debugger and have Python 2.7.3.
    >
    > When running this I have an error when trying to remove cpy_lst[4]... and ONLY THAT!!! Even in interactive mode!!!


    Several points here. You've written a beautiful O(N^2) duplicates
    remover... Python has a really fast way of doing it, if you don't mind
    losing order:

    cpy_lst = list(set(lst))

    But let's assume you're doing this for the exercise. Your technique is
    fine, if inefficient on large lists, but the remove() method looks for
    the first occurrence of an element by its value - what you want is:

    del cpy_lst

    which will remove one element by index.

    With that change, you'll have a slightly odd duplicate remover that
    keeps the *last* of any given element. That's rather unusual. Instead,
    you may want to consider maintaining a set of "items I've already
    seen", and keeping all elements that aren't in that set. I won't give
    you all the code, but here's the basic set operations:

    sighted = set()
    sighted.add(some_element)
    if some_element in sighted: # condition is True if you've already
    seen this element

    Hope that helps!

    ChrisA
     
    Chris Angelico, Nov 20, 2012
    #2
    1. Advertising

  3. Alvaro Combo

    Alvaro Combo Guest

    Dear Chris,

    Thank you very much for you reply...
    For a newcomer to Python the Hell is in the details... :).

    You are absolutely right... and I just used the index instead of the value.

    Regarding you other (most relevant) comments, I absolutely agree... BUT in those cases... I was aware :). But since it is in fact an exercise... and having N^2 operations... is not important. Still your comments are quite pertinent.

    Once again, Thank you and best regards

    ACombo
     
    Alvaro Combo, Nov 20, 2012
    #3
  4. Alvaro Combo

    Alvaro Combo Guest

    Dear Chris,

    Thank you very much for you reply...
    For a newcomer to Python the Hell is in the details... :).

    You are absolutely right... and I just used the index instead of the value.

    Regarding you other (most relevant) comments, I absolutely agree... BUT in those cases... I was aware :). But since it is in fact an exercise... and having N^2 operations... is not important. Still your comments are quite pertinent.

    Once again, Thank you and best regards

    ACombo
     
    Alvaro Combo, Nov 20, 2012
    #4
  5. Alvaro Combo

    Terry Reedy Guest

    On 11/20/2012 9:14 AM, Chris Angelico wrote:
    > On Wed, Nov 21, 2012 at 12:56 AM, Alvaro Combo <> wrote:
    >> Hi All,
    >>
    >> I'm relatively new to Python... but I have found something I cannot explain... and I'm sure you can help me.
    >>
    >> I have the following function that serves for removing the duplicates from a list... It's a simple and (almost) trivial task.
    >>
    >> I'm using WingIDE as editor/debugger and have Python 2.7.3.
    >>
    >> When running this I have an error when trying to remove cpy_lst[4]... and ONLY THAT!!! Even in interactive mode!!!

    >
    > Several points here. You've written a beautiful O(N^2) duplicates
    > remover... Python has a really fast way of doing it, if you don't mind
    > losing order:
    >
    > cpy_lst = list(set(lst))
    >
    > But let's assume you're doing this for the exercise. Your technique is
    > fine, if inefficient on large lists, but the remove() method looks for
    > the first occurrence of an element by its value - what you want is:
    >
    > del cpy_lst
    >
    > which will remove one element by index.
    >
    > With that change, you'll have a slightly odd duplicate remover that
    > keeps the *last* of any given element. That's rather unusual. Instead,
    > you may want to consider maintaining a set of "items I've already
    > seen", and keeping all elements that aren't in that set. I won't give
    > you all the code, but here's the basic set operations:
    >
    > sighted = set()
    > sighted.add(some_element)
    > if some_element in sighted: # condition is True if you've already
    > seen this element


    The itertools doc, in the last section, has a recipe for this problem
    that uses the above approach.


    --
    Terry Jan Reedy
     
    Terry Reedy, Nov 20, 2012
    #5
  6. Alvaro Combo wrote:

    >
    > Hi All,
    >
    > I'm relatively new to Python... but I have found something I cannot explain... and I'm sure you can help me.
    >
    > I have the following function that serves for removing the duplicates from a list... It's a simple and (almost)
    > trivial task.
    >
    > I'm using WingIDE as editor/debugger and have Python 2.7.3.
    >
    > When running this I have an error when trying to remove cpy_lst[4]... and ONLY THAT!!! Even in interactive
    > mode!!!
    >
    > Any suggestion is MOST welcome.
    >
    > Best Regards
    >
    > ACombo
    >
    >...And the code:
    >
    > def remove_dup_5_10():
    > """
    > Remove the duplicates of a given list. The original list MUST be kept.
    > """
    >
    > # Set the original list
    > lst = ['a', 1, 10.0, 2, 'd', 'b', 'b', 'b', 1, 2, 'b' ]
    >
    > # NEED to create a copy... See dicussion on Problem 5.6 and issue #2
    > cpy_lst = list(lst)
    >
    > # Perform an infinite loop... explained later
    > i=0 # initialize the index
    > while i != len(cpy_lst):
    > if cpy_lst.count(cpy_lst) != 1:
    > cpy_lst.remove(i)
    > else:
    > i += 1
    >
    > print "The original List: ", lst
    > print "List with NO duplicates: ", cpy_lst
    >
    > return True
    > --


    Remove looks for the *value*not the *index* of the value.


    >>> help([].remove)

    Helpon built-in function remove:

    remove(...)
    L.remove(value) -- remove first occurrence of value.
    Raises ValueError if the value is not present.

    Change ` cpy_lst.remove(i)` to `cpy_lst.remove(cpy_lst)`.

    ~Ramit



    This email is confidential and subject to important disclaimers and
    conditions including on offers for the purchase or sale of
    securities, accuracy and completeness of information, viruses,
    confidentiality, legal privilege, and legal entity disclaimers,
    available at http://www.jpmorgan.com/pages/disclosures/email.
     
    Prasad, Ramit, Nov 20, 2012
    #6
  7. On Wed, Nov 21, 2012 at 1:37 AM, Alvaro Combo <> wrote:
    > Dear Chris,
    >
    > Thank you very much for you reply...
    > For a newcomer to Python the Hell is in the details... :).


    You're most welcome! As Adam Savage of Mythbusters is fond of saying
    (with an exaggerated accent), "It's all a learning experience".

    > Regarding you other (most relevant) comments, I absolutely agree... BUT in those cases... I was aware :). But since it is in fact an exercise... and having N^2 operations... is not important. Still your comments are quite pertinent.


    Yep. O(N^2) isn't inherently a problem, it just means that the
    technique will scale poorly to large numbers of list elements. With
    small lists, it'll be "fast enough" - for all intents and purposes,
    the bulk of Python code executes in zero time, despite being in an oh
    so slow interpreted language and using ridiculously inefficient (but
    beautifully readable) code. That's the beauty of modern hardware :)

    However, I do think that people should be aware when they're writing
    non-scaleable code. That's not just algorithmic complexity; if you're
    making a system that won't handle more than one request a second
    (because, for instance, it uses seconds-since-1970 as a request
    identifier), that's something worth being aware of, even if it's
    unlikely ever to be a problem. Just know, so that if a problem ever
    _does_ occur, you know where it is!

    ChrisA
     
    Chris Angelico, Nov 20, 2012
    #7
  8. On Tue, 20 Nov 2012 05:56:27 -0800 (PST), Alvaro Combo
    <> declaimed the following in
    gmane.comp.python.general:


    > ...And the code:
    >
    > def remove_dup_5_10():
    > """
    > Remove the duplicates of a given list. The original list MUST be kept.
    > """
    >
    > # Set the original list
    > lst = ['a', 1, 10.0, 2, 'd', 'b', 'b', 'b', 1, 2, 'b' ]
    >
    > # NEED to create a copy... See dicussion on Problem 5.6 and issue #2
    > cpy_lst = list(lst)
    >


    Why copy the source list at the start and then...

    > # Perform an infinite loop... explained later
    > i=0 # initialize the index
    > while i != len(cpy_lst):
    > if cpy_lst.count(cpy_lst) != 1:
    > cpy_lst.remove(i)


    .... remove elements from the copy...


    > else:
    > i += 1
    >
    > print "The original List: ", lst
    > print "List with NO duplicates: ", cpy_lst
    >


    Wouldn't it be simpler to just add each NON-duplicate of the source
    to the destination?

    dest = []
    for itm in source:
    if itm not in dest:
    dest.append(itm)

    --
    Wulfraed Dennis Lee Bieber AF6VN
    HTTP://wlfraed.home.netcom.com/
     
    Dennis Lee Bieber, Nov 21, 2012
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Simon-Pierre  Jarry
    Replies:
    2
    Views:
    2,394
    Henrik
    Aug 10, 2005
  2. Sean Berry
    Replies:
    2
    Views:
    285
    Larry Bates
    May 6, 2004
  3. tshad
    Replies:
    6
    Views:
    21,496
    tshad
    Aug 8, 2006
  4. Rares Vernica

    remove a list from a list

    Rares Vernica, Nov 17, 2006, in forum: Python
    Replies:
    1
    Views:
    261
    J. Clifford Dyer
    Nov 20, 2006
  5. Rares Vernica

    Re: remove a list from a list

    Rares Vernica, Nov 17, 2006, in forum: Python
    Replies:
    4
    Views:
    309
    John Henry
    Nov 17, 2006
Loading...

Share This Page