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


Groups > comp.lang.python > #33687

Re: Problem with list.remove() method

From Dennis Lee Bieber <wlfraed@ix.netcom.com>
Subject Re: Problem with list.remove() method
Date 2012-11-20 19:08 -0500
Organization > Bestiaria Support Staff <
References <b610616c-c26d-44e1-84c6-fa922a8e3f75@googlegroups.com>
Newsgroups comp.lang.python
Message-ID <mailman.120.1353456507.29569.python-list@python.org> (permalink)

Show all headers | View raw


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/

Back to comp.lang.python | Previous | NextPrevious in thread | Find similar | Unroll thread


Thread

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

csiph-web