Hacking Node
Hacking Node

Reputation: 442

Index of element from the list

I need to process a list of emails, cut them and make the output with counter of iterations

MailList = [[email protected], [email protected], [email protected]] # I have a list with emails
# I need to process them and get next output: "User%number% is email1" I'm using something like:
for c in MailList:
    print('user'+ ' is '+c[0:7]) # what I need to insert here to get desirable counter for my output?

Upvotes: 0

Views: 64

Answers (3)

Hackaholic
Hackaholic

Reputation: 19733

using itertools.takewhile:

>>> import itertools
>>> MailList = ['[email protected]', '[email protected]', '[email protected]']
>>> for x in MailList:
...     print("user is {}".format("".join(itertools.takewhile(lambda x:x!='@',x))))
... 
user is email1
user is email2
user is email3

using index:

>>> for x in MailList:
...     print("user is {}".format(x[:x.index('@')]))
... 
user is email1
user is email2
user is email3

using find:

>>> for x in MailList:
...     print("user is {}".format(x[:x.find('@')]))
... 
user is email1
user is email2
user is email3

Upvotes: 0

rlms
rlms

Reputation: 11060

If I understand correctly, you want this:

MailList = ["[email protected]", "[email protected]", "[email protected]"]

for index, value in enumerate(MailList):
    print('user {} is {}'.format(index, value.split("@")[0]))

See the docs for details on enumerate.

Upvotes: 0

Kasravnd
Kasravnd

Reputation: 107287

You need to split the emails with @ :

>>> MailList = ['[email protected]', '[email protected]', '[email protected]']
>>> for i in MailList :
...   print ('user'+ ' is {}'.format(i.split('@')[0]) )
... 
user is email1
user is email2
user is email3

Upvotes: 1

Related Questions