what if there is no space between names eg . ‘WilliamCarlosWilliams’
then you need a different solution, looking at capitalized letters for example.
can enyone tell what is the problem?
That looks fine, what is the problem you are experiencing?
i couldn’t pass instruction number 2 because i wrote “author_lastname” instead of “author_last_name”.
the absence of one “_” was the problem.
thanks
Didn’t realise name.split()[-1] was valid syntax. Jesus that would have been easier.
I did this weird thing where I ended up having to convert a 2d list into a normal list using a list comprehension
def lastname(authorslist):
lastnames = []
for authors in authorslist:
lastnames.append(authors.split())
for i in range(len(lastnames)):
lastnames[i].pop(0)
lastnames = [j for sub in lastnames for j in sub]
return lastnames
author_last_names = lastname(author_names)
Though I suppose this function doesn’t really work if the person has a middle name.
Edit: fixed my way of doing it anyway:
def lastname(authorslist):
lastnames = []
for authors in authorslist:
lastnames.append(authors.split())
for i in range(len(lastnames)):
for items in lastnames[i]:
if len(lastnames[i]) == 1:
continue
if len(lastnames[i]) == 2:
lastnames[i].pop(0)
elif len(lastnames[i]) > 2:
for itemcount in lastnames[i]:
lastnames[i].pop(0)
lastnames = [j for sub in lastnames for j in sub]
return lastnames
I had this same confusion. It became more clear to me when I read the Codecademy solution above explaining that each time the loop iterates, the name.split() function call returns a list in the form of [‘firstname’, ‘lastname’]. It still surprises me a bit, though, that you can place the [-1] index right after the function call. I didn’t remember that [‘firstname’, ‘lastname’] [-1] is proper syntax. I am more used to using the index after a list name, e.g. something like names[-1] for a list called names.
I didn’t know name.split()[-1] was valid either!
Not as elegant as some of the one line list comprehensions above but here is my solution:
author_names = authors.split(',')
author_names_split = [author_names[i].split() for i in range(len(author_names))]
author_last_names = [author_names_split[i][-1] for i in range(len(author_names_split))]
Hey there,
Here’s a comprehension that eliminates the need for an interim “authors_names” list:
author_last_names = [name.split()[-1] for name in authors.split(“,”)]
I think I’m starting to comprehend comprehensions
I think you should update the learning materials because it was not show how to slice something using split
Here’s what I came up with. Seems pretty easy after I figured it out:
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 = [name.split(" ")[1] for name in author_names]
print(author_last_names)
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(",")
both_names = []
for name in author_names:
both_names.append(name.split())
author_last_names = [b for (a, b) in both_names]
print(author_last_names)
I just wanted to share my code of this exercice:
Thank you for taking your time to explain this!
Here is another solution, I might have missed above.
author_last_names = [ full_name.split()[1] for full_name in authors.split(',')]
Hi all,
This was a really interesting problem to solve and I thought I would share some info that helped me
I hope this helps
Hi, I solved this using list comprehension with the original string authors
.
Is there an alternative method to do this?
feel like giving up. I would have never figured this ou