Can't access the key from my hashtable python
<p>So I have a hash table class which is fully functioning, now I want to define another function let say will take a parameter of hash_table and print out every keys from my hash table where </p>

<pre><code>def to_print(hash_table):
    for a in hash_table:
            if a is not None:
                    print(a)
</code></pre>

<p>I was told that I need an iterator, so I define an iter function like below:</p>

<pre><code>def __iter__(self):
    for item in self.array:
            if item is not None:
                    (key, value)=item
                    return key    
</code></pre>

<p>However, I still can't read through my keys in my hash table.Error is happened at my line 'for a in hash_table:' For the iterator, if I change 'return key' to 'print key', it does print out every key. And I didn't define <strong>next</strong> because I think I don't need it(?)</p>

<p>Can anyone tell me  where's the problem and maybe some hints of it? Thanks in advance.</p>

<p>Eg. my hash table consist of &lt;'apple',6>, &lt;'orange', 7>, my output should print out apple and orange.</p>

<p><code>__iter__</code> must return iterator, try to do something like:</p>

<pre><code>def __iter__(self):
    return (item[0] for item in self.array if item is not None)
</code></pre>

python class iterator hashtable