title: How to conditionally add key/value pairs to a dictionary within a function call?
body: <p>I'm attempting to retrieve data from a Jira database and save to a Pandas data frame. Here's my code:</p>
  <pre><code>from jira import JIRA
  import pandas as pd
  
  cert_path = 'C:\\cert.crt'
  
  start_date = '2020-10-01'
  end_date = '2020-10-31'
  
  # three different instances (each with their own schema)
  a_session = JIRA(server='https://jira.myinstance-A.com', options={'verify': cert_path}, kerberos=True)
  
  b_session = JIRA(server='https://jira.myinstance-B.com', options={'verify': cert_path}, kerberos=True)
  
  c_session = JIRA(server='https://jira.myinstance-C.com', options={'verify': cert_path}, kerberos=True)
  
  
  # define Jira queries
  query_1 = 'project = \&quot;Test Project 1\&quot; and issuetype = Incident and resolution = Resolved and updated &gt;= {} and updated &lt;= {}'.format(start_date, end_date)
  
  query_2 = 'project = \&quot;Test Project 2\&quot; and issuetype = Incident and resolution = Resolved and updated &gt;= {} and updated &lt;= {}'.format(start_date, end_date)
  
  query_3 = 'project = \&quot;Test Project 3\&quot; and issuetype = Defect and resolution = Resolved and releasedate &gt;= {} and releasedate &lt;= {}'.format(start_date, end_date)
  
  query_4 = 'project = \&quot;Test Project 4\&quot; and issuetype = Enhancement and resolution = Done and completed &gt;= {} and completed &lt;= {}'.format(start_date, end_date)
  
  
  # fetch all issues from a given session for a given query
  block_size = 100
  block_num = 0
  
  
  def get_all_issues(session, query):
  
      block_size = 50
      block_num = 0
      
      start = 0
      all_issues = []
      while True:
          issues = session.search_issues(query, start, block_size)
          if len(issues) == 0:
              # No more issues
              break
          start += len(issues)
          for issue in issues:
              all_issues.append(issue)
  
      issues = pd.DataFrame(issues)
  
      for issue in all_issues:
          d = {
              'jira_key' : issue.key,
              'issue_type' : issue.fields.type,
              'creator' : issue.fields.creator,
              'resolution' : issue.fields.resolution
               }
  
          issues = issues.append(d, ignore_index=True)
  
      return issues
  
  
  # list of queries, and the corresponding backend
  queries = [
      (a_session, query_1),
      (a_session, query_2),
      (b_session, query_3),
      (c_session, query_4),
  ]
  
  
  # loop over each pair of session and query, calling the get_all_issues function, and save the dataframe we get each time
  dataframes = []
  
  for session, query in queries:
      dataframe = get_all_issues(session, query)
      dataframes.append(dataframe)
  
  
  # concatenate all data frames
  all = pd.concat(dataframes)
  </code></pre>
  <p>This code works just fine (because the <code>jira_key</code>, <code>issue_type</code>, <code>creator</code>, and <code>resolution</code> field names in the <code>d</code> dict are common to <code>a_session</code>, <code>b_session</code>, and <code>c_session</code>).</p>
  <p>The problem arises when I try to introduce a custom field that might be present in, say, <code>a_session</code> but not in <code>b_session</code> or <code>c_session</code>.</p>
  <p>For example:</p>
  <pre><code>for issue in all_issues:
      d = {
          'key' : issue.jira_key,
          'type' : issue.fields.issue_type,
          'creator' : issue.fields.creator,
          'resolution' : issue.fields.resolution,
          'system_change' : issue.fields.custom_field_123,  # only applicable to a_session and b_session
          'system_resources' : issue.fields.custom_field_456,  # only applicable to c_session
          'system_backup' : issue.fields.custom_field_789   # only applicable to b_session and c_session
           }
  </code></pre>
  <p><code>custom_field_123</code> exists in <code>a_session</code> and <code>b_session</code>, but not in <code>c_session</code>.</p>
  <p><code>custom_field_456</code> exists only in <code>c_session</code>.</p>
  <p>And, <code>custom_field_789</code> exists in <code>b_session</code> and <code>c_session</code>.</p>
  <p>Running the code with this expanded dictionary results in the following error: <code>AttributeError: type object 'PropertyHolder' has no attribute 'custom_field_123'.</code></p>
  <p>I tried to add an <code>IF</code> statement before <code>for issue in all_issues:</code>, but that approach didn't work.</p>
  <p>Is there a way to add key/value pairs to the <code>d</code> dictionary based on whether the field is relevant to a given <code>session</code>? For example, as <code>a_session/query_2</code> is running, only consider <code>custom_field_123</code> (because that's the only one that's valid) and <strong>disregard</strong> <code>custom_field_456</code> and <code>custom_field_789</code> (because passing these would result in the error message above).</p>