Append a Python List Using a List Comprehension

In some code with which I am working at the moment, I need to be able to generate a list of labels based on a variable number that I provide elsewhere in a script. In this case, I am working with the Sci-Kit Learn’s topic modeling functions, and as I work iteratively through a given corpus, I am regularly adjusting the number of topics I think “fit” the corpus. Elsewhere in the script, I am using pandas to create a dataframe that contains the names of the texts as row labels and then the topic numbers will be used as column labels.

df_lda_DTM = pd.DataFrame(data= lda_W, index = docs, columns = topic_labels)

In the script, I simply use n_components to specify the number of topics which which the function, LDA or NMF, is to work.

I needed some way to generate the topic labels on the fly so that I would not be stuck with manually editing this:

topic_labels = ["Topic 0", "Topic 1", "Topic 2"]

I was able to do so with a for loop that looked like this:

topic_labels = []
for i in range(0, n_components):
    instance = "Topic {}".format(i)

Eventually, it dawned on me that range only needs the upper bound, so I could drop the 0 inside the parenthesis:

topic_labels = []
for i in range(n_components):
    topic_labels.append("Topic {}".format(i))

That works just fine, but, while not a big block of code, this piece is part of a much longer script, and if I could get it down to a single line, using a list comprehension, I would make the overall script much easier to read, since this is just a passing bit of code that does one very small thing. One line should be enough.

Enter Python’s list comprehension, a bit of syntax sugar, as pythonistas like to call it, that I have by no means, er, fully comprehended. Still, here’s an opportunity to learn a little bit more.

So, following the guidelines for how you re-block your code within a list comprehension, I tried this:

topic_labels = [topic_labels.append("Topic {}".format(i)) for i in range(n_components)]

Better coders than I will recognize that this will not work, and will return a list of [None, None, None].

But appending a list is simply one way of building a list, of adding elements to a list, isn’t it? I could use Python’s string addition to pull this off, couldn’t I? Yes, yes I could, and did:

topic_labels = ["Topic " + str(i) for i in range(n_components)]

It couldn’t be simpler, and shorter. And it works:

['Topic 0', 'Topic 1', 'Topic 2']

Leave a Reply