Path: csiph.com!usenet.pasdenom.info!news.albasani.net!newsfeed.freenet.ag!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!194.109.133.87.MISMATCH!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.009 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; '21,': 0.07; 'python': 0.09; 'list...': 0.09; 'subject:method': 0.09; 'assume': 0.11; 'slightly': 0.15; 'duplicates': 0.16; 'element.': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'helps!': 0.16; 'index.': 0.16; 'losing': 0.16; 'set()': 0.16; 'subject:Problem': 0.16; 'task.': 0.16; 'wingide': 0.16; 'wed,': 0.16; 'wrote:': 0.17; 'duplicate': 0.17; 'element': 0.17; 'odd': 0.17; 'code,': 0.18; 'written': 0.20; 'all,': 0.21; 'trying': 0.21; 'fine,': 0.22; 'elements': 0.23; "i've": 0.23; 'header:In- Reply-To:1': 0.25; 'looks': 0.26; 'am,': 0.27; 'change,': 0.27; 'set.': 0.27; 'message-id:@mail.gmail.com': 0.27; 'points': 0.29; "i'm": 0.29; 'basic': 0.30; 'function': 0.30; 'error': 0.30; 'running': 0.32; "aren't": 0.33; 'instead,': 0.33; 'to:addr :python-list': 0.33; 'received:google.com': 0.34; 'nov': 0.35; 'doing': 0.35; "won't": 0.35; 'received:209.85': 0.35; 'something': 0.35; 'really': 0.36; 'but': 0.36; 'method': 0.36; 'subject:with': 0.36; 'keeps': 0.37; 'maintaining': 0.37; 'rather': 0.37; 'received:209': 0.37; 'subject:: ': 0.38; 'sure': 0.38; 'several': 0.39; 'to:addr:python.org': 0.39; 'header:Received:5': 0.40; 'help': 0.40; 'your': 0.60; 'remove': 0.61; "you've": 0.61; 'first': 0.61; "you'll": 0.62; 'relatively': 0.62; 'alvaro': 0.84; 'combo': 0.84; 'exercise.': 0.84; 'order:': 0.84; 'sighted': 0.84; 'inefficient': 0.91; 'technique': 0.93 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; bh=SLtI/mGEZKB1UJo2PaNaZSld2rp1fZz6TREYXiFVLjI=; b=mvhbTNy5P+SLkvBHmK2lA3kKR0w3rmVMcqStGE0+d4Q5ol0cTc+G4MpPf1xzt+T2KL DmG4ZYJncKVyyiRq6i4KGw+q2O4IaFMzS4ulRYg6kWJA28w3XcBGHekUaMffZKtmnKbd JxKS5nqC90eMsOTXxlKccX7mF3LeU5p6dU7Ktv13mguj6dIK5KcjJF9OJ5jBn/U/L92V bmYfAxa7UvV8UIrWC29XZaYtKdNanI4KoNFthRRqxBwU2bOJBMTG1Ljnenna+DZAkP3A xIF5pFcW6FPMHcoLKONQxgqC6vMXIHs51jdyV5H2PmBHEPulXnMhum7lTLdIcCRGbvUl ATzA== MIME-Version: 1.0 In-Reply-To: References: Date: Wed, 21 Nov 2012 01:14:38 +1100 Subject: Re: Problem with list.remove() method From: Chris Angelico To: python-list@python.org Content-Type: text/plain; charset=ISO-8859-1 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 39 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1353420881 news.xs4all.nl 6854 [2001:888:2000:d::a6]:46516 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:33617 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[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