user9265709
user9265709

Reputation:

Adapt print function formatting to skip missing values

I am looking to have a standard python function where if it doesn't see the title/value then it just skips that and prints the rest.

In other words, let's suppose I have three values Job , Dept and Designation, and if the Job is missing then skip that and print the other two.

Is there a way in python or python3 to do the job?

Here's an example:

>>> print("Job: %s\nDept: %s\nDesignation: %s" %('cad', 'tl', 'it'))
Job: cad
Dept: tl
Designation: it

Upvotes: 2

Views: 80

Answers (1)

jpp
jpp

Reputation: 164773

This is one way. It assumes a value is "missing" if it is not True, e.g. 0, False, empty string, None.

def printer(cat, val):
    for i, j in zip(cat, val):
        if j:
            print('{0}: {1}'.format(i, j))

cats = ['Job', 'Dept', 'Designation']

printer(cats, ['cad', 'tl', 'it'])
# Job: cad
# Dept: tl
# Designation: it

printer(cats, ['cad', '', 'it'])
# Job: cad
# Designation: it

Upvotes: 2

Related Questions