Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!feeder.news-service.com!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!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.002 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'instance,': 0.05; 'sadly': 0.07; 'iterate': 0.09; 'tuple': 0.09; 'pm,': 0.11; 'output': 0.12; 'def': 0.13; 'wrote:': 0.14; '[])': 0.16; 'empty"': 0.16; 'numpy': 0.16; 'semanchuk': 0.16; 'set,': 0.16; '\xa0for': 0.16; '\xa0print': 0.16; 'subject:list': 0.22; 'header:In-Reply-To:1': 0.22; '\xa0if': 0.23; 'received:209.85.161.46': 0.26; 'received :mail-fx0-f46.google.com': 0.26; 'function': 0.27; 'message- id:@mail.gmail.com': 0.28; '"the': 0.28; 'definition': 0.29; 'received:209.85.161': 0.29; 'fri,': 0.29; 'list': 0.30; 'to:addr :python-list': 0.32; 'fails': 0.35; 'list,': 0.36; 'received:209.85': 0.37; 'received:google.com': 0.38; 'but': 0.38; 'to:addr:python.org': 0.39; 'received:209': 0.39; "it's": 0.40; 'header:Received:5': 0.40; 'philip': 0.60; '2011': 0.62; 'care': 0.67 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:in-reply-to:references:from:date :message-id:subject:to:content-type:content-transfer-encoding; bh=WxriBIQa0QBekxQkrJxHJsXZMyAraULenEkFGO7158Q=; b=fo8CjiuWekLj5fL2u1cYBM4H2row593j9V0BVgJuqZ2n2hIeGEOupoDK0cVRa3EO2r I7SlW2JFYRY0E1UebUHhN1xHNJmFHXGc0xlsW8po3V8e9bpbqrgI+kv+n1mNpgkMLGNf 5jb621Td7gyMkzmPnz/9N0iTf7cnfann+OHgU= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type:content-transfer-encoding; b=T5aIccku1080ZcqD8ygpvVfy2QNr4KIVFUnW5yf1WWGy8CJK/74ICngiuOTCsBJLUn RcUNe8LVWS8zkvWUVQQF/BwXmqsWkVF9TY3lO1q+VJpfkTROHP/OfMec1d6Lz5g0xHSW 7stmuzp7Ka8U1QtYJNillMg8DXRT3jXybDA0Y= MIME-Version: 1.0 In-Reply-To: <8BD5792C-AFE3-4B86-82DB-B6C3F249FA5F@semanchuk.com> References: <200e93c2-6b87-4113-9c6f-85815e51ea77@28g2000yqu.googlegroups.com> <8BD5792C-AFE3-4B86-82DB-B6C3F249FA5F@semanchuk.com> From: Ian Kelly Date: Fri, 6 May 2011 17:51:44 -0600 Subject: Re: checking if a list is empty To: Python Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable 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: 23 NNTP-Posting-Host: 82.94.164.166 X-Trace: 1304725936 news.xs4all.nl 81485 [::ffff:82.94.164.166]:34504 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:4875 On Fri, May 6, 2011 at 4:21 PM, Philip Semanchuk wro= te: > What if it's not a list but a tuple or a numpy array? Often I just want t= o iterate through an element's items and I don't care if it's a list, set, = etc. For instance, given this function definition -- > > def print_items(an_iterable): > =A0 =A0if not an_iterable: > =A0 =A0 =A0 =A0print "The iterable is empty" > =A0 =A0else: > =A0 =A0 =A0 =A0for item in an_iterable: > =A0 =A0 =A0 =A0 =A0 =A0print item > > I get the output I want with all of these calls: > print_items( list() ) > print_items( tuple() ) > print_items( set() ) > print_items( numpy.array([]) ) But sadly it fails on iterators: print_items(xrange(0)) print_items(-x for x in []) print_items({}.iteritems())