title: Using a Variable to Access the Key of a Python Dictionary
body: <p>I am working with dictionaries that will be used to track several dollar balances.  For this I used the following dictionary, contained inside a class:</p>
  
  <p><code>principal = {"A": 2998.41, "B": 5794.58, "C": 3806.04, "D": 4190.39, "E": 4638.05, "F": 1827.17, "G": 4618.19, "H": 1737.77, }
  priority = {"A": 6, "B": 5, "C": 2, "D": 1, "E": 4, "F": 3, "G": 8, "H": 7}</code></p>
  
  <p>These will eventually reach zero, but since this could happen at varying times I need a way to find out which one has been paid down, when.  As part of a class function, I have the following code:</p>
  
  <pre><code>            for key, value in self.principal.items():
  
              # Check to ensure a pmt would not cause a group's balance to be paid off and become negative.
              # If it would, apply the excess to the next-highest priority group.
  
              if (self.principal[key] * (1 + self.int_monthly[key])) - self.pmts[key] &lt;= 0:
  
                  del (self.priority[self.groups[key]])
  
                  excess = round((self.pmts[key] - self.principal[key] * (1 + self.int_monthly[key])), 2)
                  print("Excess from group "+str(self.principal[key])+": $"+str(excess))
  </code></pre>
  
  <p>pmts and int_monthly are also dictionaries, and the calculations work fine because their values are pulled by accessing dict[key], but with the delete and print statements, for example, I want to know what <strong>key</strong> has been paid and delete it from the dictionary, not return the current value of that key.</p>
  
  <p>As it stands, this code throws a KeyError because there is no 'D' key in the dictionary "priority."  For the life of me nothing about dictionaries seems to allow me to access the key based on some variable value like this.</p>
  
  <p>It would be easy if I knew which key I wanted to access ahead of time (I would just say self.principal["key"]).  But I'm beginning to think after looking around here and Google that there might not be a way to do this just using dictionaries.  Is there a common workaround to this problem?</p>