How to select only last names?

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()
author_last_names.append(last[-1])
return author_last_names

Hi,

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.

‘author_last_names.append(i.split()[-1])’

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!

I am having trouble understanding why wrapping the solution in a function is not working.

This does not work, but removing the def last_names(author_names): line (and un-indenting the for loop, of course) does work.

author_last_names = []

def last_names(author_names):

  for name in author_names:

  author_last_names.append(name.split()[-1])

print(author_last_names)
1 Like

It’s because Python functions do not have a write binding to a global. Try declaring the list in your function, then return that to the caller once it is populated with data.

def ...
    last_names_list = []
    # ...
    return last_names_list

author_last_names = last_names(author_names)
2 Likes

Hi this code and exercise is very useful for understanding and splitting list items but the lesson could have been introduced clearer and more sequentially. The overabundance of posts may be an indicator that the lesson is confusing from the perspective of introducing a new concept in py.

2 Likes

That was very helpful. Thank you so much. That was a great explanation. :clap: :clap: :smile:

I am also confused by this. How does it know when we use split()[1] or [-1] that it’s accessing the list of characters from the last name and not the first? And if we wanted to access the entire last name, wouldn’t we use [:-1] and not just [-1]? I thought [-1] would just give us one single character in the list.

accessing elements by index in a list gives use the element in the list

accessing elements by index of a string gives us a single character.

I’d never have thought of adding [-1] to split()

this is my solution

authors = "Audre Lorde, William Carlos Williams, 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)

author_last_names = []
for name in author_names:
  fullname_list = name.split()
  lastName = fullname_list[-1]
  author_last_names.append(lastName)

print(author_last_names)





Codecademy used to give questions of the same kind where we need to dig deep for the answer but this one was really out of lane. This can be prevented had they have another lesson after the said question and introduce the concept there. If I will find the answer on the forum, I might as well just read forums and stop paying.

How about this?

authors = "Audre Lorde, William Carlos Williams, 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 name in author_names:
  author_last_names.append(name[name.rfind(" "):].strip())
1 Like

Here is my solution:

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(",")

#create an empty list for the last names
author_last_names = []
#loop through each of the names in author_names
for name in author_names:
  #split each name spaces. Creates a list with first_name and last_name
  split_name = name.split()

  #append the last name to the author_last_names list
  author_last_names.append(split_name[1])

print(author_last_names)

As the task didn’t strictly say use append I used list comprehension instead. Seemed like the cleanest way to do it. are there any pro’s/cons to doing it this way instead?

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

A list comprehension seems sensible to me but you’ll have problems with some names given your current indexing method, e.g. “William Carlos Williams”.

1 Like

Ahh true I should index at -1 instead. Thank you. :facepunch:t5:

2 Likes

thank you so much for this

1 Like