简体   繁体   中英

How do I join elements of tuples of a list through list comprehension

I have a list containing tuples of three elements which contain e-mail data.

email_data = [('jbd', 'email', '.com'), ('my_jbd', 'my_site', '.com')]

What I am trying to attain is to join three elements of each tuple and get email address like 'jbd@email.com' and for that I am using list comprehension as under:

email_list = [  (y+'@', y) [i!=0] for x in result for i, y in enumerate(x) ]
print( email_list ) # ['jbd@', 'email', '.com', 'my_jbd@', 'my_site', '.com']

What I exactly require is list like this -> ['jbd@email.com', 'my_jbd@my_site.com'].
Since, I am a beginner in Python, I am clueless as to how do I join these tuple elements within list comprehension and get a list containing email data. Please guide me.

Use the format method.

email_list = ["{}@{}{}".format(*t) for t in email_data]

Use:

email_data=[i[0]+"@"+i[1]+i[2] for i in email_data]
print(email_data)

This should work:

[f'{el[0]}@{el[1]}{el[2]}' for el in email_data]

What we are doing here is using a format string to pass in each tuple which we are naming 'el' in the list comprehension.

Try:

email_data = [('jbd', 'email', '.com'), ('my_jbd', 'my_site', '.com')]
for i in email_data :
    s = i[0] + '@'+''.join(i[1:])
    print(s)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM