Second level looping over dictionaries
<p>In building a class with an out line like below I would like the behaviour of  the for loops to, if done once: just give the keys as normal an then move on to the next line of code. But if a second loop is set up inside the first loop it would give the keys on the first loop and then ea value in the sequences in the second loop.  The problem I can't figure out is how to set up this under <strong>iter</strong>.     </p>

<pre><code>class MyClass():
    def __init__(self):
        self.cont1 = [1,2,3,4]
        self.cont2 = ('a','b','c')

def __iter__(self):
    pass # ???????
</code></pre>

<p>Something like this:</p>

<pre><code>dct = dict(container1=[5,6,7,8], container2=('a','b','c')

if one loop is used:

   for ea in dct:
        print(ea)
print("Howdy")

'containter1'
'containter2'
Howdy
</code></pre>

<p>If a nest loop is used:</p>

<pre><code>for ea in dct:
    print(ea)
    for i in dct.get(ea):
        print(i)

'container1'
5
6
...
'container2'
a
b
c
</code></pre>

<p>You cannot do that simply by implementing <code>__iter__</code>. <code>__iter__</code> should return an <em>iterator</em>, that is, an object that keeps the state of an iteration (the current position in a sequence of items) and has a method <code>next</code> that returns with each invocation the next item in the sequence.</p>

<p>If your object has nested sequences you can implement an iterator that will traverse only the external sequence, or one that will traverse both
the external and the internal sequences - in a depth-first or a breath-first fashion - but it does not make sense to use nested loops on the same iterable:</p>

<pre><code># iterate over every item in myobj
for x in myobj:
    ...
    # iterate over every item again? not likely what you want!
    for y in myobj:
</code></pre>

<p>A more likely situation is:</p>

<pre><code>for x in myob:
    ...
    for y in x:
        ...
</code></pre>

python class for-loop