Path: csiph.com!usenet.pasdenom.info!news.albasani.net!newsfeed.freenet.ag!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.031 X-Spam-Evidence: '*H*': 0.94; '*S*': 0.00; 'value,': 0.03; 'exist,': 0.07; 'subject:How': 0.09; 'exists,': 0.09; 'formatted': 0.09; 'cc:addr:python-list': 0.10; 'example).': 0.16; 'fld': 0.16; 'specified.': 0.16; 'subject:exists': 0.16; 'wrote:': 0.17; 'skip': 0.17; 'define': 0.20; 'work,': 0.22; "i'd": 0.22; 'cc:2**0': 0.23; 'elements': 0.23; 'thus': 0.24; 'cc:no real name:2**0': 0.24; 'cc:addr:python.org': 0.25; 'header:In-Reply- To:1': 0.25; 'header:User-Agent:1': 0.26; '(most': 0.27; 'set.': 0.27; "doesn't": 0.28; 'end,': 0.29; 'case,': 0.29; 'probably': 0.29; 'e.g.': 0.30; 'could': 0.32; 'print': 0.32; 'like:': 0.33; 'another': 0.33; 'list': 0.35; 'exist': 0.35; 'pm,': 0.35; 'subject:?': 0.35; 'something': 0.35; 'there': 0.35; 'list.': 0.35; 'add': 0.36; 'but': 0.36; 'compare': 0.36; 'available.': 0.37; 'beyond': 0.37; 'does': 0.37; 'skip:z 10': 0.37; 'subject:: ': 0.38; 'some': 0.38; 'nothing': 0.38; 'received:192': 0.39; 'received:192.168': 0.40; 'end': 0.40; 'day,': 0.60; 'between': 0.63; 'series': 0.63; 'more': 0.63; 'become': 0.65; 'header:Reply- To:1': 0.68; 'received:74.208': 0.71; 'reply-to:no real name:2**0': 0.72; 'day': 0.73; 'balance,': 0.84; 'inclusive': 0.84; 'subscripts': 0.84; 'approach.': 0.91 Date: Thu, 06 Sep 2012 14:18:05 -0400 From: Dave Angel User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:14.0) Gecko/20120714 Thunderbird/14.0 MIME-Version: 1.0 To: tinnews@isbd.co.uk Subject: Re: How to print something only if it exists? References: <9s4nh9-8dr.ln1@chris.zbmc.eu> In-Reply-To: <9s4nh9-8dr.ln1@chris.zbmc.eu> Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit X-Provags-ID: V02:K0:at5XH/L2z4aaV0PbynvEpDkPCg+IktPEe1ubCSYPkfB ms/ATELpqDWvq6X9l9C9yk0E64wk43zzLxEgZvT0tNtO6V/mIA k2OSc8oQdSQ7Gr63Z4bnpYZlbgS7oWGDhFheqX8EFsXgBNfrCu OxqBu9ajdn5p7sa6UUkTMMXcfQflTW3GvxxDJjvMM8OFAld23o i38hzMyLUzY1hXi+abp9dSjJM8fPX3Fw4262FtRz7oPeYc/y71 l/k0ETsOWUAQmrdcEAB6W1mXjWXW8W9GlYPAUjf928E3r+kVHb AvaDJ/tC+iz6zwDJgycIey62GDRM2KTGLChsLwT8CUN6TIeKg= = Cc: python-list@python.org X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list Reply-To: d@davea.name 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: 43 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1346955517 news.xs4all.nl 6929 [2001:888:2000:d::a6]:41138 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:28625 On 09/06/2012 01:59 PM, tinnews@isbd.co.uk wrote: > I want to print a series of list elements some of which may not exist, > e.g. I have a line:- > > print day, fld[1], balance, fld[2] > > fld[2] doesn't always exist (fld is the result of a split) so the > print fails when it isn't set. > > I know I could simply use an if but ultimately there may be more > elements of fld in the print and the print may well become more > complex (most like will be formatted for example). Thus it would be > good if there was some way to say "print this if it exists". > Would you like to define "exists" ? A list is not sparse, so all items exist if their subscript is less than the length of the list. So all you need to do is compare 2 to len(fld). But perhaps there's another approach. Just what DO you want to print if fld(1) exists, but fld(2) does not? Do you still want to print out day, fld(1), and balance? Or do you want to skip balance as well? if you literally want nothing printed for list elements beyond the end, then I'd add some extra empty-strings to the end of the list. fld.extend("" * 5) Now, subscripts 0 through 4 inclusive will work, as specified. Alternatively, perhaps there's some association between the day and the fld(1), and the balance and the fld(2). in that case, probably you want to make a loop out of it, and only print each pair if they're both available. Something like: for tag, value in zip((something, day, balance), fld): print tag, value, "" -- DaveA