title: [Python]Function that runs once then remembers result when called again
body: <p>I have a function that builds a list from a very large file. That list is used several times at various stages. Obviously, it only needs to runs once and running it more would waste time. </p>
  
  <p>My question is how to have a function run once and return the list but when that function is called again it simply returns the list and doesn't run again.</p>
  
  <p>I'd rather not just make the list a global variable. I know I could use a while loop to remember it as well but that just seems wasteful. I assume there is a more elegant way of accomplishing what I'm looking for.</p>
  
  <p>Am I looking to use decorators or singletons (not sure if they are in python).</p>
  
  <p>Example:</p>
  
  <pre><code>state_ab = ['OH', "test", 'CO']
  
  
  def build_US_state_list():
      state_dict = []
      for state in pycountry.subdivisions:
          if state.country_code == 'US':
              state_dict.append({'name': state.name, 'code': state.code[3:]})
      return state_dict
  
  
  def get_US_state_list():
      try:
          return state_dict
      except:
          state_dict = build_US_state_list()
          print('ran')
          return state_dict
  
  
  def identify_and_add_US_states():
      states = get_US_state_list()
      print(states)
      for i in range(0, len(state_ab)):
          for state in states:
              if state_ab[i] == state["code"]:
                  return state["code"]
  </code></pre>