Adding new values to empty nested lists
<p>This is related to <a href="http://stackoverflow.com/questions/6339235/how-to-append-to-the-end-of-an-empty-list">How to append to the end of an empty list?</a>, but I don't have enough reputation yet to comment there, so I posted a new question here.</p>

<p>I need to append terms onto an empty list of lists. I start with:</p>

<pre><code>Talks[eachFilename][TermVectors]=
      [['paragraph','1','text'],
       ['paragraph','2','text'],
       ['paragraph','3','text']]
</code></pre>

<p>I want to end with </p>

<pre><code>Talks[eachFilename][SomeTermsRemoved]=
      [['paragraph','text'],
       ['paragraph','2'],
       ['paragraph']]
</code></pre>

<p><code>Talks[eachFilename][SomeTermsRemoved]</code> starts empty. I can't specify that I want: </p>

<pre><code>Talks[eachFilename][SomeTermsRemoved][0][0]='paragraph'
Talks[eachFilename][SomeTermsRemoved][0][1]='text'
Talks[eachFilename][SomeTermsRemoved][1][0]='paragraph'
</code></pre>

<p>etc... (IndexError: list index out of range).  If I force populate the string  and then try to change it, I get a strings are immutable error.</p>

<p>So, how do I specify that I want <code>Talks[eachFilename][SomeTermsRemoved][0]</code> to be <code>['paragraph','text']</code>, and <code>Talks[eachFilename][SomeTermsRemoved][1]</code> to be <code>['paragraph','2']</code> etc?</p>

<p><code>.append</code> works, but only generates a single long column, not a set of lists.</p>

<p>To be more specific, I have a number of lists that are initialized inside a dict</p>

<pre><code>Talks = {}
Talks[eachFilename]= {}
Talks[eachFilename]['StartingText']=[]
Talks[eachFilename]['TermVectors']=[]
Talks[eachFilename]['TermVectorsNoStops']=[]
</code></pre>

<p><code>eachFilename</code> gets populated from a list of text files, e.g.:</p>

<pre><code>Talks[eachFilename]=['filename1','filename2']
</code></pre>

<p><code>StartingText</code> has several long lines of text (individual paragraphs)</p>

<pre><code>Talks[filename1][StartingText]=['This is paragraph one','paragraph two']
</code></pre>

<p>TermVectors are populated by the NLTK package with a list of terms, still grouped in the original paragraphs:</p>

<pre><code>Talks[filename1][TermVectors]=
     [['This','is','paragraph','one'],
      ['paragraph','two']]
</code></pre>

<p>I want to further manipulate the <code>TermVectors</code>, but keep the original paragraph list structure. This creates a list with 1 term per line:</p>

<pre><code>for eachFilename in Talks:
    for eachTerm in range( 0, len( Talks[eachFilename]['TermVectors'] ) ):
        for term in Talks[eachFilename]['TermVectors'][ eachTerm ]:
            if unicode(term) not in stop_words:
                Talks[eachFilename]['TermVectorsNoStops'].append( term )
</code></pre>

<p>Result (I lose my paragraph structure):</p>

<pre><code>Talks[filename1][TermVectorsNoStops]=
     [['This'],
      ['is'],
      ['paragraph'],
      ['one'],
      ['paragraph'],
      ['two']]
</code></pre>

<p>Some continued experimentation, along with the comments got me moving towards a solution. Rather than appending each individual term, which generates a single long list, I accumulated the terms into a list and then appended each list, as follows:</p>

<pre><code>for eachFilename in Talks:
    for eachTerm in range( 0, len( Talks[eachFilename]['TermVectors'] ) ):
        term_list = [ ]
        for term in Talks[eachFilename]['TermVectors'][ eachTerm ]:
            if unicode(term) not in stop_words:
                term_list.append(term)
        Talks[eachFilename]['TermVectorsNoStops'].append( term )
</code></pre>

<p>Thanks everyone!</p>

python list assign