Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!feeder2.ecngs.de!ecngs!feeder.ecngs.de!xlned.com!feeder1.xlned.com!newsfeed.xs4all.nl!newsfeed6.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.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'string.': 0.04; 'append': 0.07; "'''": 0.09; '[];': 0.09; 'assumed': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'def': 0.10; 'index': 0.13; 'indexerror:': 0.16; 'messy': 0.16; 'received:80.91.229.3': 0.16; 'received:dip.t-dialin.net': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-dialin.net': 0.16; 'string': 0.17; 'wrote:': 0.17; 'string,': 0.17; '>>>': 0.18; 'python?': 0.20; '"",': 0.22; 'header:User-Agent:1': 0.26; '(most': 0.27; 'possibility': 0.27; 'instead.': 0.27; 'header:X-Complaints-To:1': 0.28; 'words': 0.29; 'code': 0.31; 'file': 0.32; 'traceback': 0.33; 'version:': 0.33; 'to:addr:python-list': 0.33; 'list': 0.35; 'fail': 0.35; 'faster': 0.35; 'received:org': 0.36; 'but': 0.36; 'should': 0.36; 'itself': 0.37; 'subject:: ': 0.38; 'to:addr:python.org': 0.39; 'header:Received:5': 0.40; 'range': 0.60; 'note:': 0.64 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Peter Otten <__peter__@web.de> Subject: Re: Strange behavior Date: Thu, 16 Aug 2012 15:02:40 +0200 Organization: None References: <1a1834ae-2b4a-473f-b626-f37a17588199@googlegroups.com> <502aeb2b$0$29978$c3e8da3$5496439d@news.astraweb.com> <502CD723.5090201@it.uu.se> Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Gmane-NNTP-Posting-Host: p50849008.dip.t-dialin.net User-Agent: KNode/4.7.3 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 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: 52 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1345122171 news.xs4all.nl 6931 [2001:888:2000:d::a6]:42309 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:27159 Virgil Stokes wrote: >>> def testFunc(startingList): >>>xOnlyList = []; j = -1 >>>for xl in startingList: >>>if (xl[0] == 'x'): >> That's going to fail in the starting list contains an empty string. Use >> xl.startswith('x') instead. > Yes, but this was by design (tacitly assumed that startingList was both a > list and non-empty). You missunderstood it will fail if the list contains an empty string, not if the list itself is empty: >>> words = ["alpha", "", "xgamma"] >>> [word for word in words if word[0] == "x"] Traceback (most recent call last): File "", line 1, in IndexError: string index out of range The startswith() version: >>> [word for word in words if word.startswith("x")] ['xgamma'] Also possible: >>> [word for word in words if word[:1] == "x"] ['xgamma'] > def testFunc1(startingList): > ''' > Algorithm-1 > Note: > One should check for an empty startingList before > calling testFunc1 -- If this possibility exists! > ''' > return([x for x in startingList if x[0] == 'x'], > [x for x in startingList if x[0] != 'x']) > > > I would be interested in seeing code that is faster than algorithm-1 In pure Python? Perhaps the messy variant: def test_func(words): nox = [] append = nox.append withx = [x for x in words if x[0] == 'x' or append(x)] return withx, nox