Jason Schayer
Jason Schayer

Reputation: 171

Unexpected return of list of lists instead of list

This app will download a webpage and find all email addresses in the text of the page and return a list of them.

This is my current code:

def emails(content):
'return list of email addresses contained in string content'
    email = []
    content = urlopen(url).read().decode()
    pattern='[A-Za-z0-9_.]+\@[A-Za-z0-9_.]+\....'
    email.append(re.findall(pattern,content))
    print(email)

But for some reason I get:

[['[email protected]"']]

instead of :

['[email protected]']

Upvotes: 0

Views: 62

Answers (1)

RocketDonkey
RocketDonkey

Reputation: 37269

re.findall actually returns a list, so you are appending a list to the list. You could do something like email.extend(re.findall(pattern,content)) if you didn't want that behavior (although I usually do checks for matches on their own line to ensure that matches are found and non-matches are handled properly).

Upvotes: 5

Related Questions