title: Trying to delete a key/value pair from JSON within a list
body: <p><a href="https://i.stack.imgur.com/cHJJe.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/cHJJe.png" alt="enter image description here" /></a></p>
  <pre><code>def loop_through_list_keys_and_update_or_delete(body_dict, list_key, key_to_manipulate, update_or_delete):
      request_body = body_dict
      try:
  
          for keys in request_body[list_key]:
              for key, value in keys.items():
                  if key == key_to_manipulate:
                      if update_or_delete == 'delete':
                          del request_body[key]
  </code></pre>
  <p>Hi all!  Relatively new to Python, so please be gentle and I definitely appreciate your help!  In the attached picture are my run time values.  Basically what I am trying to do is delete a specific (but one that changes) key/value pair.  The request_body is my json that I've read in.</p>
  <p>I am looping on the list 'fxTransactions' and the only keys it is getting are financialTransaction and transactionRefId.  In this case, I need to get to the request_body[fxTransactions][0][financialTransaction][rule]  field that is within financialTransaction and delete it and its value.  But I can't send this as a variable to the delete as you can't combine the list name with the keys in a variable.  I can't seem to figure out how to get to this field and delete it.  Note that the field/value pair to delete is dynamic so I will not always know which one I want to delete (could be debtor, creditor, etc).  I'd also like to use this code for other lists, if possible, so trying not to hard-code.  But I can if there is no other way.</p>
  <p>Thank you in advance!</p>
  <p>Here is code that works to do what I'm trying to do, but I'm trying to come up with a way to make it cleaner and reusable:</p>
  <pre><code>index_key_int = int(index_key)
  split_key = key.split(&quot;.&quot;)
  keys_numb = len(split_key)
  if keys_numb == 5:
      del request_body[list_key][index_key_int][split_key[0]][split_key[1]][split_key[2]][split_key[3]][
          split_key[4]]
  if keys_numb == 4:
      del request_body[list_key][index_key_int][split_key[0]][split_key[1]][split_key[2]][split_key[3]]
  if keys_numb == 3:
      del request_body[list_key][index_key_int][split_key[0]][split_key[1]][split_key[2]]
  elif keys_numb == 2:
      del request_body[list_key][index_key_int][split_key[0]][split_key[1]]
  elif keys_numb == 1:
      del request_body[list_key][index_key_int][split_key[0]]
  </code></pre>