Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #33655
| From | Terry Reedy <tjreedy@udel.edu> |
|---|---|
| Subject | Re: Problem with list.remove() method |
| Date | 2012-11-20 15:32 -0500 |
| References | <b610616c-c26d-44e1-84c6-fa922a8e3f75@googlegroups.com> <CAPTjJmqqc+_BaoHVouO-9RBxehtfNY=aWSUDK6e0isbYe0C_RQ@mail.gmail.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.90.1353443565.29569.python-list@python.org> (permalink) |
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
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll 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