I have a list of websites, and I want to extract social media profiles only (let's say facebook, linkedin and pinterest)
import numpy as np
mylist = ['linkedin.com/profilexyz','facebook.com/profile374','bbcnews.com/USA_news','stackoverflow.com']
I have used list comprehension to get the urls, returning nan
if it's not found:
facebook = [x for x in mylist if 'facebook' in x else np.nan for x in mylist]
linkedin = [x for x in mylist if 'linkedin' in x else np.nan for x in mylist]
pinterest = [x for x in mylist if 'pinterest' in x else np.nan for x in mylist]
However I get the error:
File "<ipython-input-329-578130619ae7>", line 1
facebook = [x for x in mylist if 'facebook' in x else np.nan for x in mylist]
^
SyntaxError: invalid syntax
I have checked the suggested duplicates such as this one: if/else in a list comprehension? but can't get my comprehension to work.