ho_howdy
ho_howdy

Reputation: 73

A function that returns email addresses from a list of strings in Python

I need to create a function which returns email addresses from a list of strings. I have done this successfully with the following code:

def myfunction(bigstring):
    str = ' '.join(bigstring)
    my_string = str
    str1 = re.findall(r'[\w\.-]+@[\w\.-]+', my_string)
    return str1

However when I run the following sample:

emails = ['John Kennedy <[email protected]> or <[email protected]>','Adam Hartley <[email protected]>','Ben Saunders <[email protected]>']
myfunction(emails)

I get the output:

['[email protected]', '[email protected]', '[email protected]', '[email protected]']

However i would like the following output, but I am not sure how to do so without messing up my code:

[['[email protected]', [email protected]'], ['[email protected]'], ['[email protected]']]

I need to return a list within a list as shown by the desired output.

Upvotes: 0

Views: 2217

Answers (5)

Sumana
Sumana

Reputation: 27

    def myfunction(bstr):
        #str = ' '.join(bstr)
        #my_string = str
        str1 = re.findall(r'[\w\.-]+@[\w\.-]+', bstr)
        return str1
    import re
    output=[]
    emails = ['John Kennedy <[email protected]> or <[email protected]>','Adam 
              Hartley <[email protected]>','Ben Saunders <[email protected]>']
     for item in emails:
        output.append(myfunction(item))

Upvotes: 0

ArunJose
ArunJose

Reputation: 2159

def myfunction(bigstring):
    #str = ' '.join(bigstring)
    #my_string = str
    str1 = re.findall(r'[\w\.-]+@[\w\.-]+', bigstring)
    return str1

import re
output=[]
emails = ['John Kennedy <[email protected]> or <[email protected]>','Adam Hartley <[email protected]>','Ben Saunders <[email protected]>']
for item in emails:
    output.append(myfunction(item))

print(output)

Upvotes: 1

Yugandhar Chaudhari
Yugandhar Chaudhari

Reputation: 3964

Dont join the whole string in function and pass the string one by one by iteration like this

import re
def myfunction(bigstring):
    return  re.findall(r'[\w\.-]+@[\w\.-]+', bigstring)

emails = ['John Kennedy <[email protected]> or <[email protected]>','Adam Hartley <[email protected]>','Ben Saunders <[email protected]>']


output = []
for emailstring in emails:
    output.append((myfunction(emailstring)))
print(output)

using list comprehension

output = [ myfunction(email) for email in emails ]
print(output)

using map

print(map(myfunction,emails))

Output

[['[email protected]', '[email protected]'], ['[email protected]'], ['[email protected]']]

Upvotes: 1

Raymond Reddington
Raymond Reddington

Reputation: 1837

You don't need to join the initial array of email containing strings.

def my_function(str_array):
    return [re.findall(r'[\w\.-]+@[\w\.-]+', s) for s in str_array]

emails = ['John Kennedy <[email protected]> or <[email protected]>','Adam Hartley <[email protected]>','Ben Saunders <[email protected]>']

my_function(emails)

Upvotes: 2

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 627082

Extract emails in each list item:

import re

emails = ['John Kennedy <[email protected]> or <[email protected]>','Adam Hartley <[email protected]>','Ben Saunders <[email protected]>']
def myfunction(bigstring):
    result = []
    for s in bigstring:
        result.append(re.findall(r'[\w.-]+@[\w.-]+', s))
    return result

print(myfunction(emails))
# => [['[email protected]', '[email protected]'], ['[email protected]'], ['[email protected]']]

See the Python demo.

Actually, bigstring is a bad name here as it is a list. Consider renaming to, say, my_list or something.

As for the regex, you do not need to escape dots inside character classes.

Upvotes: 1

Related Questions