How to select only last names?

I’m a new member here and I can’t find this question anywhere and you can’t send messages to mods either.

My question is, what do I need to do to start a new topic?

I don’t think there’s anything specific you need to do before that’s an option. It is however worthwhile following the tutorial with @discobot before adding a new thread. Bear in mind that a lot of the mods and almost all of the users here are either experienced volunteers who are generous with their time or fellow learners (by and large the majority of users fit this category). Please do check the following link beforehand as it details a lot of information that makes it easier for both you and any responders-

Hi! To find out what I can do, say @discobot display help.

for name in author_names:
  author_last_names.append(name[name.index(" ") + 1:])


I looked at the solution for the problem because I had figured there was probably a more elegant solution. Thankfully, I was right! This is what my original answer was. My thought process was to find the index of the " " or space in between the first name and last name and then add whatever was after that.

The problem with this, just like using [1] instead of [-1] in the solution is that if there was a middle name, it would add both the middle name and name to author_last_names.

I love coming into the forums to see explanations of the solutions or seeing other’s thought processes because it allows me to learn more than the lessons teach and I can reflect back on my own work! Good luck to anyone else learning or just brushing up on topics!

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

count = 0
for i in author_names:
lst = author_names[count].split()
count += 1

This was my solution. I added a comma to the end of the authors string.

Hey guys,

Correct me if I am wrong but:
Wouldn’t name.split() generate a new list with “First name A”, “Last name a”, “First name B”, “Last name B”…?

Therefore, wouldn’t name.split()[-1] end up choosing the last thing on the list aka “Last name Z”?

Or does the [-1] refer exclusively for name.spilt()?

Thanks all :slight_smile:

Could you include your full code? Otherwise, answering this questions becomes a lot of guessing

if you have something like:

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

for name in author_names:

.split() is called on a string (elements of the list, thanks to the for loop). So then, you would be good.

Could someone please explain


  1. authors_first_name = [ ] && 2) authors_first_name = " "

I was thinking both used for empty string.

the first one create a list, not an empty string
the second is not an empty string. The string contains a space. "" is an empty string, " " is a string containing a white space.

1 Like

@stetim94 Thank you very much sir/mam :slight_smile:

Great explanation. Thank you.
Is there a way to get other posts from you.

you didn’t reply to anyone in particular, but I am sure who-ever it is, is grateful :slight_smile:

yes, you can click on someones user name, this shows a small popup, then you can click on the username again, taking you to there profile, where you can view all activity

Excellent Explanation !

can you explain better how this works "name.split()[-1]

split() will split the string into a list, using negative indexes we can access the list from the right hand side. so -1 is the last element in the list

Thanks a lot! Now it is quite clear.

as mentioned in the solution -

This confused me for about 45 minutes. Not sure how a beginner would figure out chaining a list index to a string method. I appreciate the advanced concepts, but it’s a little frustrating that ideas in the solution weren’t explored in the lesson.