How to print something only if it exists?

T

tinnews

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

Dave Angel

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, ""
 
E

Emile van Sebille

On 9/6/2012 10:59 AM (e-mail address removed) said...
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 may be better off ensuring that fld is an appropriate length. One
way may be tweaking the split to return the right numbers of elements:
T = "1,2,3"
flds = (T+","*5).split(",")[:5]
flds
['1', '2', '3', '', '']

Emile
 
T

Terry Reedy

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

Hans Mulder

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.

How about:

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


If you really want to avoid the keyword 'if', then you'd have to
do something like:

print day, fld[1], balance, (fld[2:3] or [''])[0]

That may be shorter, but it isn't very readable.


Hope this helps,

-- HansM
 
R

Roy Smith

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

One possible way is a trick I've used in the past.

fld = split(...) + ['']*10

this guarantees that fld has at least 10 elements. If you want to
guarantee that fld has *exactly* 10 elements, just take [0:10] of that.
 
T

tinnews

Dave Angel said:
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).
Yes, a I said a simple len(fld) will tell me if fld[2] 'exists' but it
gets messy if I have to do it in the middle of the print sequence.

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?
Here's a sample of the file whose lines are being split() :-

01 JB 0.00 Start of 2012, Initial balance
02 BB 0.00
13 ZB 0.00

I want to print out everything, it's just that in some cases there's
no descriptive text (the bit that ends up in fld[2]).

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.
That's probably the simplest approach, thank you.
 
D

Dave Angel

Dave Angel said:
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).
Yes, a I said a simple len(fld) will tell me if fld[2] 'exists' but it
gets messy if I have to do it in the middle of the print sequence.

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?
Here's a sample of the file whose lines are being split() :-

01 JB 0.00 Start of 2012, Initial balance
02 BB 0.00
13 ZB 0.00

I want to print out everything, it's just that in some cases there's
no descriptive text (the bit that ends up in fld[2]).

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.
That's probably the simplest approach, thank you.

If there literally is only one missing field, and at the end, then you
could use fld.append("") instead. Or better, you could do something like
if len(fld) == 2 : fld.append(""")

This may be longer, but at least it's in one place -- the place where
the split is occurring. And it pretty much states that the fld2 is
optional.

You ought to consider what error to report if you encounter a line with
missing fields that ARE required.
 
P

Prasad, Ramit

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top