Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #33613 > unrolled thread

Problem with list.remove() method

Started byAlvaro Combo <alvaro.combo@gmail.com>
First post2012-11-20 05:56 -0800
Last post2012-11-20 19:08 -0500
Articles 8 — 5 participants

Back to article view | Back to comp.lang.python


Contents

  Problem with list.remove() method Alvaro Combo <alvaro.combo@gmail.com> - 2012-11-20 05:56 -0800
    Re: Problem with list.remove() method Chris Angelico <rosuav@gmail.com> - 2012-11-21 01:14 +1100
      Re: Problem with list.remove() method Alvaro Combo <alvaro.combo@gmail.com> - 2012-11-20 06:37 -0800
        Re: Problem with list.remove() method Chris Angelico <rosuav@gmail.com> - 2012-11-21 07:48 +1100
      Re: Problem with list.remove() method Alvaro Combo <alvaro.combo@gmail.com> - 2012-11-20 06:37 -0800
    Re: Problem with list.remove() method Terry Reedy <tjreedy@udel.edu> - 2012-11-20 15:32 -0500
    RE: Problem with list.remove() method "Prasad, Ramit" <ramit.prasad@jpmorgan.com> - 2012-11-20 20:47 +0000
    Re: Problem with list.remove() method Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2012-11-20 19:08 -0500

#33613 — Problem with list.remove() method

FromAlvaro Combo <alvaro.combo@gmail.com>
Date2012-11-20 05:56 -0800
SubjectProblem with list.remove() method
Message-ID<b610616c-c26d-44e1-84c6-fa922a8e3f75@googlegroups.com>
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[i]) != 1:
            cpy_lst.remove(i)
        else:
            i += 1
        
    print "The original List: ", lst
    print "List with NO duplicates: ", cpy_lst
            
    return True

[toc] | [next] | [standalone]


#33617

FromChris Angelico <rosuav@gmail.com>
Date2012-11-21 01:14 +1100
Message-ID<mailman.55.1353420881.29569.python-list@python.org>
In reply to#33613
On Wed, Nov 21, 2012 at 12:56 AM, Alvaro Combo <alvaro.combo@gmail.com> 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[i]

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

[toc] | [prev] | [next] | [standalone]


#33618

FromAlvaro Combo <alvaro.combo@gmail.com>
Date2012-11-20 06:37 -0800
Message-ID<9caaea9c-8b7c-4ed5-bc3b-2519a58ceb26@googlegroups.com>
In reply to#33617
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

[toc] | [prev] | [next] | [standalone]


#33657

FromChris Angelico <rosuav@gmail.com>
Date2012-11-21 07:48 +1100
Message-ID<mailman.93.1353444493.29569.python-list@python.org>
In reply to#33618
On Wed, Nov 21, 2012 at 1:37 AM, Alvaro Combo <alvaro.combo@gmail.com> 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

[toc] | [prev] | [next] | [standalone]


#33619

FromAlvaro Combo <alvaro.combo@gmail.com>
Date2012-11-20 06:37 -0800
Message-ID<mailman.57.1353422242.29569.python-list@python.org>
In reply to#33617
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

[toc] | [prev] | [next] | [standalone]


#33655

FromTerry Reedy <tjreedy@udel.edu>
Date2012-11-20 15:32 -0500
Message-ID<mailman.90.1353443565.29569.python-list@python.org>
In reply to#33613
On 11/20/2012 9:14 AM, Chris Angelico wrote:
> On Wed, Nov 21, 2012 at 12:56 AM, Alvaro Combo <alvaro.combo@gmail.com> 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[i]
>
> 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

[toc] | [prev] | [next] | [standalone]


#33662

From"Prasad, Ramit" <ramit.prasad@jpmorgan.com>
Date2012-11-20 20:47 +0000
Message-ID<mailman.98.1353445988.29569.python-list@python.org>
In reply to#33613
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[i]) != 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)
Help on 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[i])`.

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

[toc] | [prev] | [next] | [standalone]


#33687

FromDennis Lee Bieber <wlfraed@ix.netcom.com>
Date2012-11-20 19:08 -0500
Message-ID<mailman.120.1353456507.29569.python-list@python.org>
In reply to#33613
On Tue, 20 Nov 2012 05:56:27 -0800 (PST), Alvaro Combo
<alvaro.combo@gmail.com> 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[i]) != 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
        wlfraed@ix.netcom.com    HTTP://wlfraed.home.netcom.com/

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web