FAQ: String Methods - Splitting Strings II

Since you are not altering the actual author_names list, you can use membership instead of indexing.

for x in author_names:
    author_last_names.append(x.split())

Thank you very much for the input. However, I think this will print the whole name (first and last) but the task is to print last names only.
Thank you

1 Like

Yeah, kind of breezed over that.

author_last_names.append(x.split()[-1])
2 Likes

Hi hoping to get something straightened out, just a general rule of thumb-
So you can not split twice, as it goes from a string (which you can split) to a list (which you can not).
Therefore, you need a ‘for’ statement to split a list, into a list of lists?

Have i got that correct?
thank you

Rather than guess, could you provide us with an example of what you wish to do?

sorry, didnt mean to be vague.
In the question referring to splitting strings:

authors = “Audre Lorde,Gabriela Mistral,Jean Toomer,An Qi,Walt Whitman,Shel Silverstein,Carmen Boullosa,Kamala Suraiyya,Langston Hughes,Adrienne Rich,Nikki Giovanni”

author_names = authors.split(‘,’)

author_last_names =
for i in author_names:
author_last_names.append(i.split()[-1])

so in the above example we split ‘authors’ into ‘author_names’. Then we were asked to get just the last names. Initially i tried to split ‘author_names’ by writing author_last_names = author_names.split().
Obviously that didn’t work. After reading up, im just trying to understand if the correct rule is - you can not split twice, as you can only split a string and not a list? And if you want to split a list you must include it inside a for statement(as in the example above creating author_last_names)?

Unless you have a str object. The methods are inherited from the type.

1 Like

This was my solution, below:

def authors_last_names(names):
  last_names = []
  for i in names:
    last_names.append(i.split()[-1])
  return last_names

print(authors_last_names(author_names))

author_last_names = authors_last_names(author_names)

@jacielbarros nice solution! I suppose you could have reversed the final two lines to make the assignment first and just print the assigned variable to tidy it up cleaner but looks great!

1 Like

Hi,

I might have been butting my head on this for too long, and I see that there is a lot of people who have asked for help with this assignment. I think I see myself tunnel-visioning a bit, so in hope to get some help, here is my code:

authors = "Audre Lorde,Gabriela Mistral,Jean Toomer,An Qi,Walt Whitman,Shel Silverstein,Carmen Boullosa,Kamala Suraiyya,Langston Hughes,Adrienne Rich,Nikki Giovanni" author_names = authors.split(',') print(author_names) def extract_last_names(full_names): f_and_l_names = [name.split(' ') for name in full_names] for names in f_and_l_names: last_names = [names[-1:] for names in f_and_l_names] return last_names author_last_names = extract_last_names(author_names) print(author_last_names)

I am pretty sure that what I am doing wrong is creating a list where each last name is in its own sublist, instead of existing as an index of one list (without sub-elements), and that this is why it comes back as being wrong. But right now I feel a bit locked. Any of you got any tips to see this problem from a different perspective? I see multiple solutions here, which seems to be good, but I want to understand how I got here, instead of just learning from a separate solution.

Thanks in advance for your time.

Line 10 is over-indented. See if that fixes your problem.

Thanks for the tip, should I consider it a general rule of thump in python to keep return at the first indention level?

It did not fix the problem though :S Still returns me a list, where each last name is in itself a sublist, instead of an element of a list.

For expected results, return is best not inside the loop, else it terminates both the loop and the function without completion.

Please post the formatted code in a reply, this time not as a Codebyte.

1 Like