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]
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”.
Ahh true I should index at -1 instead. Thank you.
thank you so much for this
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:])
print(author_last_names)
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
author_last_names=
for i in author_names:
lst = author_names[count].split()
author_last_names.append(lst[-1])
count += 1
print(author_last_names)
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
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:
name.split()
.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
THE DIFFERENCE BETWEEN
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.
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
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]