Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #28629
| From | Terry Reedy <tjreedy@udel.edu> |
|---|---|
| Subject | Re: How to print something only if it exists? |
| Date | 2012-09-06 15:34 -0400 |
| References | <9s4nh9-8dr.ln1@chris.zbmc.eu> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.322.1346960078.27098.python-list@python.org> (permalink) |
On 9/6/2012 1: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. What fails is the indexing operation. You can prevent that by slicing rather than indexing: fld[1:2], fld[2:3]. These will give '' if there is no fld[1], fld[2]. Whether or not this is sensible depends on what you want the output to look like in these cases. -- Terry Jan Reedy
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll 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