Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #29060

RE: How to print something only if it exists?

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <ramit.prasad@jpmorgan.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.004
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; 'completeness': 0.07; 'exist,': 0.07; 'subject:How': 0.09; 'formatted': 0.09; 'received:155': 0.09; 'disclaimers': 0.16; 'disclaimers,': 0.16; 'example).': 0.16; 'fld': 0.16; 'from:addr:jpmorgan.com': 0.16; 'received:155.180': 0.16; 'received:159.53': 0.16; 'received:bankone.net': 0.16; 'received:exchad.jpmchase.net': 0.16; 'received:jpmchase.com': 0.16; 'received:jpmchase.net': 0.16; 'received:svr.bankone.net': 0.16; 'securities,': 0.16; 'statement.': 0.16; 'subject:exists': 0.16; 'url:disclosures': 0.16; 'url:jpmorgan': 0.16; 'wrote:': 0.17; 'to:name:python- list@python.org': 0.20; 'elements': 0.23; 'received:169.254': 0.24; 'thus': 0.24; 'header:In-Reply-To:1': 0.25; '(most': 0.27; 'accuracy': 0.27; 'set.': 0.27; "doesn't": 0.28; 'received:169': 0.29; 'e.g.': 0.30; 'header:Received:8': 0.30; 'could': 0.32; 'print': 0.32; 'to:addr:python-list': 0.33; 'list': 0.35; 'exist': 0.35; 'subject:?': 0.35; 'there': 0.35; 'but': 0.36; 'charset:us- ascii': 0.36; 'subject:: ': 0.38; 'some': 0.38; 'to:addr:python.org': 0.39; 'day,': 0.60; 'series': 0.63; 'information,': 0.63; 'url:email': 0.63; 'more': 0.63; 'become': 0.65; 'legal': 0.65; 'subject': 0.66; 'purchase': 0.67; 'sale': 0.76; 'balance,': 0.84; 'received:169.254.8': 0.84
X-DKIM OpenDKIM Filter v2.1.3 sz2.jpmchase.com q8DHmIPk014499
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=jpmorgan.com; s=smtpout; t=1347558499; bh=CIg61cS+GnfyJozOSIoe2cXfo5XLnYJR1NNXSDHi5F8=; h=From:To:Subject:Date:Message-ID:References:In-Reply-To: Content-Transfer-Encoding:MIME-Version:Content-Type; b=sq3m3Y2dQNBAIFNPPGTCiyiw55xbUiNJWRl4A1JRhZ5Ds4rMOF/9oyNeQf2WooYp+ 3kgpJltbish7KQfR0UzC+NDPCaygDYoTxR/Dj3X/iartmts7WUsK/dh9hzDLvGSwM8 UNDg0atA7N0DZFe55fHR0HuyDT8zNhUzV/kF9u98=
From "Prasad, Ramit" <ramit.prasad@jpmorgan.com>
To "python-list@python.org" <python-list@python.org>
Subject RE: How to print something only if it exists?
Thread-Topic How to print something only if it exists?
Thread-Index AQHNjFoqx2N+Mknpa0O9RK2rpfTRFJeIlnxw
Date Thu, 13 Sep 2012 17:48:06 +0000
References <9s4nh9-8dr.ln1@chris.zbmc.eu>
In-Reply-To <9s4nh9-8dr.ln1@chris.zbmc.eu>
Accept-Language en-US
Content-Language en-US
X-MS-Has-Attach
X-MS-TNEF-Correlator
x-originating-ip [10.67.79.47]
Content-Transfer-Encoding quoted-printable
MIME-Version 1.0
X-DLP-FWD Yes
Content-Type text/plain; charset="us-ascii"
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.625.1347559650.27098.python-list@python.org> (permalink)
Lines 16
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1347559650 news.xs4all.nl 6853 [2001:888:2000:d::a6]:39916
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:29060

Show key headers only | View raw


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".

You can use an inline if-else statement.

print day, fld[1], balance, fld[2] if len(fld) >= 3 else ''


Ramit
--

This email is confidential and subject to important disclaimers and
conditions including on offers for the purchase or sale of
securities, accuracy and completeness of information, viruses,
confidentiality, legal privilege, and legal entity disclaimers,
available at http://www.jpmorgan.com/pages/disclosures/email.  

Back to comp.lang.python | Previous | NextPrevious in thread | Find similar | Unroll thread


Thread

How to print something only if it exists? tinnews@isbd.co.uk - 2012-09-06 18:59 +0100
  Re: How to print something only if it exists? Emile van Sebille <emile@fenx.com> - 2012-09-06 11:19 -0700
  Re: How to print something only if it exists? Dave Angel <d@davea.name> - 2012-09-06 14:18 -0400
    Re: How to print something only if it exists? tinnews@isbd.co.uk - 2012-09-08 11:02 +0100
      Re: How to print something only if it exists? Dave Angel <d@davea.name> - 2012-09-08 07:08 -0400
  Re: How to print something only if it exists? Terry Reedy <tjreedy@udel.edu> - 2012-09-06 15:34 -0400
  Re: How to print something only if it exists? Hans Mulder <hansmu@xs4all.nl> - 2012-09-07 11:37 +0200
  Re: How to print something only if it exists? Roy Smith <roy@panix.com> - 2012-09-07 09:16 -0400
  RE: How to print something only if it exists? "Prasad, Ramit" <ramit.prasad@jpmorgan.com> - 2012-09-13 17:48 +0000

csiph-web