title: Implicit Iteration through Python dictionary elements
body: <p>I am trying to figure out if there is an implicit way to iterate through list elements. Example: with the list <code>someList = ['a','b','c']</code> you do not need to write a <code>for</code> loop to iterate through the list printing elements when <code>print(someList)</code> does an <em>implicit</em> iteration.</p>
  
  <p>I have the following (simplified) python code:</p>
  
  <pre><code>class foo:
      def __init__(self):
          self.x=None
          self.y=None
          self.z=None
  
      def set(self, x, y , z):
          self.x = x
          self.y = y
          self.z = z
  
      def get(self):
          return {'x':self.x, 'y':self.y, 'z':self.z}
  
  
  class bar:
      def __init__(self):
          self.foos=[]
      def add(self, foo):
          self.foos.append(foo)
      def get(self):
          return {'foos':self.foos}
  </code></pre>
  
  <p>Which are initialized using </p>
  
  <pre><code>f = foo()
  f.set(1,2,3)
  
  g = foo()
  g.set(4,5,6)
  
  
  h = bar()
  h.add(f)
  h.add(g)
  </code></pre>
  
  <p>Then calling</p>
  
  <pre><code>print(f.get())
  print(g.get())
  print(h.get())
  </code></pre>
  
  <p>which gives </p>
  
  <pre><code>{'z': 3, 'x': 1, 'y': 2}
  {'z': 6, 'x': 4, 'y': 5}
  {'foos': [&lt;__main__.foo object at 0x0000000003541748&gt;, &lt;__main__.foo object at 0x000000000358C630&gt;]}
  </code></pre>
  
  <p>What I'm ultimately looking to do is return a JSON like structure or Nested dictionary such that the returns of the <code>foo.get()</code> method are included in the call to <code>bar.get()</code>.</p>
  
  <p>I realize the array doesn't contain actual <code>foo</code> objects (only references to them). </p>
  
  <p>I thought about using <strong>iterators</strong> like they were explained at <a href="http://anandology.com/python-practice-book/iterators.html" rel="nofollow noreferrer">http://anandology.com/python-practice-book/iterators.html</a> . The problem with the example is that the items to be iterated over must be defined <em>within</em> the datastructure (i.e <code>foo</code>). The problem with including the iterator in the <code>bar</code> class is that <code>foos</code> list may not be the only collection to iterate over: I could have added collection <code>foos2</code> (but did not for simplicity)</p>
  
  <p>I considered the solution at <a href="https://stackoverflow.com/questions/14692690/access-python-nested-dictionary-items-via-a-list-of-keys">Access nested dictionary items via a list of keys?</a> , but the <code>pprint</code> gives me something similar to my own code. I am also not certain use of the <code>reduce</code> function would apply to my situation.</p>
  
  <p>I also thought about doing <strong>operator overloading</strong> on the <code>print</code> function, but I'm not looking to get a string, just a data structure that easy to display but also navigate.</p>
  
  <p>Is there a <strong>easy way</strong> or <strong>correct way</strong> to do this? Am I using the correct data-structure (i.e. should I be using something other than nested lists)?</p>
  
  <p>I'm still new to Python so pleas be gentle.</p>