title: Is there a better way to retrieve values than to iterate through the same loops?
body: <p>To put it simply, I have a list of <code>server</code> classes, which contain a list of <code>component</code> classes, which contain a list of <code>function</code> classes. You see where this is going.</p>
  
  <p>In my program I want to retrieve a value from each of the function classes, then use all of those values <em>on</em> each other, then add the resulting values back to the <code>function</code> classes. I end up repeatedly using the nested loops over and over.</p>
  
  <p>Here's my code:</p>
  
  <pre><code>component_subscriptions = {}
  
  for server in self.network.servers:
      for component in server.components:
          for function in component.functions:
              component_subscriptions[function.name] = []
  
  for server in self.network.servers:
      for component in server.components:
          for function in component.functions:
              for subscription in function.subscriptions:
                  if subscription.name in component_subscriptions.keys():
                      component_subscriptions[subscription.name].append(function.publish_address)
  
  for server in self.network.servers:
      for component in server.components:
          for function in component.functions:
              print(function.name)
              if function.name in component_subscriptions.keys():
                  function.subscribers.extend(component_subscriptions[function.name])
  
  for server in self.network.servers:
      for component in server.components:
          for function in component.functions:
              if function.name in component_subscriptions.keys():
                  function.subscribers = component_subscriptions[function.name]
  
  </code></pre>
  
  <p>Is there a way of reusing por simplifying the three-deep for loops? Am I missing something ridiculously obvious?</p>