How do i get list instead of list of list?
<p>I am trying to read a text file with the following format <code>"a b c d e f g h"</code>. I take a new empty list <code>word = []</code></p>

<p>My code is:</p>

<pre><code>f = open("letters.txt")

word = []

for line in f:
    line = line.split(" ")
    word.append(line)
print(word)
</code></pre>

<p>However, it has given me a list of list like this:</p>

<pre><code>[['a', 'b', 'c', 'd', 'e', 'f']]
</code></pre>

<p>But I want to get it in a single list instead?</p>

<p>eg:</p>

<pre><code>['a', 'b', 'c']
</code></pre>

<p>You should know what data is in each variable in each line of the code. If you don't know - print it and then you will know.</p>

<p>I'll do it for you this time ;)</p>

<pre><code>f = open("letters.txt")
# f is an open file object. BTW, you never close it.
# Consider: with open("letters.txt", "rt") as f:

word = []
# 'word' is a list. That is strange, why not 'words = []'?

for line in f:
    # 'line' is a string. Fine.

    line = line.split(" ")

    # 'line' is no longer a string. Not fine.
    # This is perfectly valid code, but bad practice.
    # Don't change the type of data stored in a variable,
    # or you'll run into problems understanding what your code does.
    # Make a new variable, if you need one, e.g. 'line_words'.

    # Anyway, 'line' is now a list of words. Whatever.

    word.append(line)

    # This added a list (line) into another list (word).
    # Now it is a list of lists. There is your error.
    # 'word += line' would do what you wanted.
</code></pre>

<p>All together:</p>

<pre><code>with open("letters.txt", "rt") as f:
    words = []
    for line in f:
        words += line.split()
</code></pre>

python list readfile