I have lots of dictionary keys with conditional values, ternary in this case:

# cover page 
data_coverPage.append({'reportCalendarOrQuarter': coverPage.reportcalendarorquarter.string if coverPage.reportcalendarorquarter else "N/A",
            'filingManager_name': coverPage.filingmanager.find_all('name')[0].text if coverPage.filingmanager.find_all('name') else "N/A",
            'filingManager_street1': coverPage.find('com:street1').string if coverPage.find('com:street1') else "N/A",
            'filingManager_city': coverPage.filingmanager.find('com:city').text if coverPage.filingmanager.find('com:city') else "N/A",
            'filingManager_stateOrCountry': coverPage.filingmanager.find('com:stateorcountry').text if coverPage.filingmanager.find('com:stateorcountry') else "N/A",
            'filingManager_zipCode': coverPage.filingmanager.find('com:zipcode').text if coverPage.filingmanager.find('com:zipcode') else "N/A",
            'reportType': coverPage.reporttype.string if coverPage.reporttype else "N/A",
            'form13FFileNumber': coverPage.form13ffilenumber.string if coverPage.form13ffilenumber else "N/A",
            'provideInfoForInstruction5': coverPage.provideinfoforinstruction5.text if coverPage.provideinfoforinstruction5 else "N/A"

# print data

# info tables
for it in bf.find_all('infotable'):
    # print it.nameofissuer.text
    data_infoTable.append({'nameOfIssuer': it.nameofissuer.text if it.nameofissuer else "N/A",
                 'titleOfClass': it.titleofclass.text if it.titleofclass else "N/A",
                 'cusip': it.cusip.text if it.cusip else "N/A",
                 'value': it.value.text if it.value else "N/A",
                 'sshPrnamt': it.shrsorprnamt.sshprnamt.text if it.shrsorprnamt else "N/A",
                 'sshPrnamtType': it.shrsorprnamt.sshprnamttype.text if it.shrsorprnamt.sshprnamttype else "N/A",
                 'investmentDiscretion': it.investmentdiscretion.text if it.investmentdiscretion else "N/A",
                 'putCall': it.putCall.string if it.putCall else "N/A",
                 'otherManager': it.otherManager.string if it.otherManager else "N/A", 
                 'sole': it.votingauthority.sole.text if it.votingauthority.sole else "N/A",
                 'shared': it.votingauthority.shared.text if it.votingauthority.shared else "N/A",
                 'none': it.votingauthority.none.text if it.votingauthority.none else "N/A"})

Is there a much shorter way to represent so many conditional values? I'm repeating "else 'N/A'" throughout that block of code. I could store "N/A" with a variable and refer to that variable instead of the raw string. Other than that, how would I keep things essentially DRY in this situation?


2 Answers 2


The easiest thing to do, seeing how similar most of these are, would be to have a lambda for the ternary if.

gettext = lambda var: var.text if var else "N/A"
'titleOfClass' : gettext(it.titleofclass)

If you do one for .string as well, then you only have a few (3?) cases where your if cannot be shortened to a lambda call. You could pass in another lambda to pull the value you want to store out of var, but for just three cases the added indirection doesn't seem useful.

  • \$\begingroup\$ Hoping I can use def() in place of lambda, but this works. Thanks! \$\endgroup\$
    – Mr_Spock
    Commented May 7, 2015 at 16:11

I think the best thing to do in this case is, if the condition is false, don't set the value in the dictionary. Then when you're looking things up you don't have to test for the magic string "N/A", just whether that key is found in the dictionary or not.

You're also repeating a lot of code, re-doing the find multiple times.

  • \$\begingroup\$ I should have probably specified that these are going into a database where the value is a cell in a row and the key is a column. I put "N/A" because all of the values have to be represented by something, even if it's a throw-away value such as "N/A." \$\endgroup\$
    – Mr_Spock
    Commented Apr 29, 2015 at 17:37
  • 2
    \$\begingroup\$ What kind of database is this? Why can't the database field be empty? Why can't it be NULL? \$\endgroup\$
    – Snowbody
    Commented Apr 29, 2015 at 18:28
  • \$\begingroup\$ MySQL -- but it's more a task-specific specification than anything. My boss has his requests. I'm sorry if this is becoming unnecessarily confusing. \$\endgroup\$
    – Mr_Spock
    Commented Apr 29, 2015 at 18:30

Not the answer you're looking for? Browse other questions tagged or ask your own question.