title: Iterating over tokens within lists within lists using for-loops in python (SpaCy)
body: <p>I'm relatively new, so I might be making some really basic mistake, but from what I understand, you would iterate over tokens within a list-within-a-list in python as follows:</p>
  
  <pre><code>for each_list in full_list:
    for each_token in each_list:
      do whatever you wannna do
  </code></pre>
  
  <p>However, when using SpaCy, it seems like the first for-loop is iterating over the tokens rather than the lists.</p>
  
  <p>So the code:</p>
  
  <pre><code>for eachlist in alice:
    if len(eachlist) &gt; 5:
       print eachlist
  </code></pre>
  
  <p><em>(where alice is a list of lists and each list is a sentence containing tokenized words)</em></p>
  
  <p>actually prints each word that is over 5 letters rather than each sentence which is longer than 5 words (which it should be doing if it was really on the "first level" for-loop. </p>
  
  <p>And the code:</p>
  
  <pre><code>newalice = []
  for eachlist in alice:
    for eachword in eachlist:
      #make a new list of lists where each list contains only words that are classified as nouns, adjectives, or verbs (with a few more specific stipulations)
      if (eachword.pos_ == 'NOUN' or eachword.pos_ == 'VERB' or eachword.pos_ == 'ADJ') and (eachword.dep_ != 'aux') and (eachword.dep_ != 'conj'):
          newalice.append([eachword])
  </code></pre>
  
  <p>returns the error: "TypeError: 'spacy.tokens.token.Token' object is not iterable."</p>
  
  <p>The reason I want to do this in the nested for-loops is that I want newalice to be a list of lists (I still want to be able to iterate over the sentences, I just wanted to get rid of words I don't care about). </p>
  
  <p>I don't know if I'm making some really basic error in my code, or if SpaCy is doing something weird, but either way I'd really appreciate any help on how to iterate over items in a list-in-a-list in SpaCy while keeping the integrity of the original lists.</p>