How to select only last names?

Here’s another solution - maybe it’s a bit convoluted, but takes fewer lines than most of the other solutions proposed

author_names = authors.split(',')

author_last_names = [name[:-1] for name in authors.split() if "," in name] + [author_names[-1].split()[-1]]

So using a list comprehension, iterate through all names, only select the names that contain “,”, then slice out the “,”, then concatenate the final last name which wouldn’t contain the “,” since it’s the final element of the author_last_names list.

1 Like

Hi, I wanted to ask a general question about the specific approach you outlined. Specifically,

  1. State your goal - what do you want to do with your data?
  2. What format is the data already in?
  3. What steps are necessary to do what you want?

seem like the 3 steps you are using to solve the problem. Is this the problem solving framework that expert programmers use when approaching any problem?

Thank you for any advice.

nope this does not do the exact job…

This code giving me the write answer but the system says it is wrong
giving me this message " Value for author_last_names did not match the expected value."

author_names = authors.split(',')
for word in author_names:
  new_word = word.split()
  author_last_names = []

Maybe you could look yourself and see if you agree with said system?
What value does that variable refer to after your code has finished, and what should it be? That’s what that message is saying is the problem, so that’s something you could look at.

If you by right answer mean that you’re looking at something being printed out, then that’s not what the error message is complaining about, if that’s the case you’re looking at the wrong thing.

And then if you do end up agreeing that this is wrong, you could start making observations about what individual operations you’re carrying out. When exactly does your code do something that you don’t agree with?

1 Like

I was trying comprehension list as well and I’ve got it with this one:

author_last_names = [ author.split()[-1] for author in author_names]

Hello digitalmaster74014,

I see the code works, but how did you decide on ‘author.split()[-1]’?
I tried so many times but it will not split the last name even when using [-1].


Hello ruby9015521395,
With this comprehension list I loop through each author in the original list; I split each authors ítem which then turns into an individual list of [name, lastname], and then I select the first negative index of each of this individual lists.

I was actually pretty close but how come mine doesn’t work? (See below).

author_last_names =
for name in author_names:

Have you tried checking what does get produced for the statements executed in your code? A couple of print statements at each step should help to make sure that the code you wrote is behaving as expected.

It’s also worth having a glance at this FAQ which describes how to format code you post to the forums so that it maintains indentation and such-

1 Like

Thanks for your help. I understand it better now.

i don’t get it. is the [-1] selected the entire last name?

Have you read some of the explanations in this topic? Hard to imaging none of those explanation helped. What do you not understand about it?


the mod is a jerk


see this topic:

How to ask good questions (and get good answers)

it even features that searching for answers first is a good idea.

What does [-1] do? Well, we can access elements in a list by index, by using negative values, we can access elements from the right hand side. So -1 is the last item of a list, -2 the second to last item of the list and so forth


Could someone remind me where we can find explanation in previous lessons that says we can write methods “.split” and index "[-1] in the same line like here: some_str.split()[-1]? Because i don’t really remember anything like that. But i do remember that there was about different methods in one line, but not indexes.
Also another solution to the second part of this exercise is by list comprehension:

author_last_names = [full_name.split()[-1] for full_name in author_names]

If i am not mistaken.

I’m having trouble finding any reference for using an index after str.split() on the internet.
I don’t doubt that it exists or think the, “how to”, is wrong.

I wrote this function to solve the problem:

def find_last_name(author_names):
author_last_names =
for name in author_names:
last = name.split()
return author_last_names


I couldn’t stop myself from admiring such a beautiful code where all the three activities of splitting with () delimiter + extracting last element with [-1] + appending to the empty list is done in a single line of code.


I was wondering how many years’ of experience is required to look our code more professional.

Thanks codecademy for inspiring.

Your code should get better constantly. Programming is a constant learning process. The more important thing (in my opinion) is when you look at code you wrote half a year to a year ago, and feel like you could do better now :slight_smile:

This was extremely helpful. Thank you for taking us through the ‘why’ just as much as the ‘how’.

Originally I was having trouble splitting the author list at the spaces, but this laid out for exactly why my code wasn’t working.

Thank you!