1

I have a Python list like:

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

And I want to extract only the strings after @ into another list directly, such as:

mylist = ['gmail.com', 'hotmail.com'...]

Is it possible? split() doesn't seem to be working with lists.

This is my try:

for x in range(len(mylist)):
  mylist[x].split("@",1)[1]

But it didn't give me a list of the output.

9
  • 1
    Sure it is. Have you tried anything yourself yet? Did you look up how to split a single value, for example? Commented Jun 8, 2017 at 22:17
  • 1
    In other words, have you tried breaking this problem down into smaller parts that you could attempt to solve one by one? Commented Jun 8, 2017 at 22:18
  • 1
    @Madno: then your post would be greatly helped if you included that attempt. Commented Jun 8, 2017 at 22:19
  • 1
    We wanna see code... :) Commented Jun 8, 2017 at 22:19
  • 1
    I added the code I tried. Commented Jun 8, 2017 at 22:20

2 Answers 2

11

You're close, try these small tweaks:

Lists are iterables, which means its easier to use for-loops than you think:

for x in mylist:
  #do something

Now, the thing you want to do is 1) split x at '@' and 2) add the result to another list.

#In order to add to another list you need to make another list
newlist = []
for x in mylist:
     split_results = x.split('@')
     # Now you have a tuple of the results of your split
     # add the second item to the new list
     newlist.append(split_results[1])

Once you understand that well, you can get fancy and use list comprehension:

newlist = [x.split('@')[1] for x in mylist]
Sign up to request clarification or add additional context in comments.

Comments

0

That's my solution with nested for loops:

myl = ['[email protected]', '[email protected]'...]
results = []
for element in myl:
    for x in element:
        if x == '@':
            x =  element.index('@')
            results.append(element[x+1:])

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.