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><a href="https://docs.python.org/2/library/stdtypes.html#str.split" rel="nofollow"><code>split</code></a> returns a list </p>

<blockquote>
  <p>Return a list of the words in the string, using sep as the delimiter string.</p>
</blockquote>

<p>This list is appended <em>as a whole</em> at the end of <code>word</code>. This is why you get a list of lists </p>

<pre><code>word [
    [ result of split of first line ]
    [ result of split of second line ]
    [ result of split of third line ]
    [ ... ]
]
</code></pre>

<p>If you want to add each element of the list, instead of appending the list as a whole, you may use <a href="https://docs.python.org/2/tutorial/datastructures.html#more-on-lists" rel="nofollow"><code>extend</code></a>, i.e.</p>

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

<p>Although you might want to strip the trailing newline with <a href="https://docs.python.org/2/library/string.html#string.rstrip" rel="nofollow"><code>rstrip</code></a>, when reading multiple lines</p>

<pre><code>a = line.rstrip().split(" ")
</code></pre>

<p>or just use <code>None</code> as word separator</p>

<blockquote>
  <p>If sep is not specified or is None, a different splitting algorithm is applied: runs of consecutive whitespace are regarded as a single separator, and the result will contain no empty strings at the start or end if the string has leading or trailing whitespace. Consequently, splitting an empty string or a string consisting of just whitespace with a None separator returns [].</p>
</blockquote>

<pre><code>a = line.split()
</code></pre>

python list readfile