append adding contents to first list item, not adding new items to list
<pre><code>def gen(filename):
    result=[]
    with open(filename) as sample:
        for line in sample.read().splitlines():
            for ch in line.split():                
                result.append(ch)
                yield ch
return result
</code></pre>

<p>If i pass in "ABCDEF", i get ["ABCDEF"] back in result, instead of ["A","B","C","D","E","F"]</p>

<p>What could be the problem?</p>

<p>Also, am i using the generator correctly? If not, what am i doing wrong? I am close to grasping the concept, but i am not quite there yet and feel that adding a list might be making the generator counterproductive</p>

<p>EDIT: Here is how i am opening the file:</p>

<pre><code>with filled_filename("ABCDEF") as fn:
        self.assertEqual(list(project.gen(f)), ["A","B","C","D","E","F"])
        print(list(project.gen(ff)))
</code></pre>

<p>I think this is what you want:</p>

<pre><code>def gen(filename):
    with open(filename) as sample:
        for line in sample.read().splitlines():
            for ch in line:
                yield ch

# Example usage:
for ch in gen('myfile.txt'):
    print("Got character '{}'.".format(ch))
</code></pre>

<p>As you said, the <code>list</code> you're building up sort of makes the generator redundant. (You're <code>yield</code>ing each character as you go, but you're also returning the complete <code>list</code>, which is a pattern I've never seen before and probably not what you want to do.)</p>

<p>The main issue with your code, though, is that I think you want to split a line into individual characters, but <code>line.split()</code> doesn't do that. Just use <code>for ch in line</code>.</p>

<p><strong>EDIT</strong></p>

<p>Trying to get somewhere closer to your code:</p>

<pre><code>def gen(filename):
    with open(filename) as sample:
        for line in sample:
            line = line.rstrip()
            for ch in line:
                yield ch

def filled_filename(text):
    with open('test.txt', 'wb') as f:
        f.write(text)
    return 'test.txt'

filename = filled_filename(b'ABCDEF')
assert list(gen(filename)) == ['A', 'B', 'C', 'D', 'E', 'F']
</code></pre>

<p>This code works and passes the assert.</p>

python