Pretty Python list comprehensions

Python list comprehensions are by far the simplest and most readable loop expressions that I have worked with. Here’s an example where I have a list of lists of lists (corpus –> documents –> sentences) where I need to remove some items (called stop_words here) from the sentences.

1
2
3
4
5
corpus = [[[word
            for word in sentence if word not in self._stop_words]
         for sentence in document]
       for document in corpus]
                                                      

Notice the array brackets added after each for expression so the program retains the same structure.

Another example, if I have to flatten such a deep list-

1
2
3
4
    return [word
          for document in self._corpus
         for sentence in document
       for word in sentence]