Search code examples
pythonpraw

Concatenating mulitple lists stored in one object


I am currently using PRAW to extract comments from a reddit page. I want to match certain words from the comment body to values from a csv file. Here is what I am working with so far:

submission.comments.replace_more(limit=None)
for comment in submission.comments.list():
    results = (re.findall(r'[A-Z]{3,5}',comment.body))
    print(results)

With an output:

[]
['HCMC']
[]
[]
['ASRT']
[]
[]
['CBBT', 'TLSS']
['LLEX']
[]

I understand that comment.body is really just a collection of lists stored within one object. Is there a way that I can concatenate the lists into one single list?


Solution

  • You can use itertools.chain.from_iterables:

    >>> from itertools import chain
    >>> list(chain.from_iterable(re.findall(r'[A-Z]{3,5}',comment.body)
                                 for comment in submission.comments.list()))
    ['HCMC', 'ASRT', 'CBBT', 'TLSS', 'LLEX']