0

I have scraped a website and retrieved the part where the author of a topic is stated. After extracting the authors, I have a list of lists of strings:

authorlist=[]
for post in topicsection: 
    authorlist.append(re.findall(r'<a href="/[Mm]ember.*?">(.*?)</a>', 
post))

>>>> [['author1'],['author2'],['author3']]

However, I want to turn this into one list of strings. Therefore I looped over the authorlist and appended the first[0] element of every list to the Authorlist. Sometimes an empty lists appears in the text, which causes an error. I therefore want to use the try-except command, in which the empty lists are ignored.

How can I tell Python to extract the first element of every list, but continue the loop if there is an empty list? I tried the following, in which the except-part is not working:

try:
    authorlist = [lijst[0] for lijst in authorlist]
except IndexError:
    pass

Thank you in advance!

Raymond Hettinger
  • 216,523
  • 63
  • 388
  • 485
Tessa
  • 19
  • 3

3 Answers3

2

You can have a conditional statement in your list comprehension

authorlist = [lijst[0] for lijst in authorlist if lijst]
Maresh
  • 4,644
  • 25
  • 30
1

Try adding a filtering condition to the list comprehension:

authorlist = [lijst[0] for lijst in authorlist if len(lijst) > 0]
Raymond Hettinger
  • 216,523
  • 63
  • 388
  • 485
1
authorlist = [lijst[0] for lijst in authorlist if lijst]

You should check if the authorlist is there. Hope this helps.

Navneet
  • 4,543
  • 1
  • 19
  • 29