How to select only last names?

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?

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(",") print(author_names) author_lastname = [] for i in author_names: last_name_list = i.split() author_lastname.append(last_name_list[-1]) print(author_lastname)

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 :slight_smile:

3 Likes

I think you should update the learning materials because it was not show how to slice something using split

1 Like

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:

authors = "Audre Lorde,Gabriela Mistral,Jean Toomer,An Qi,Walt Whitman,Shel Silverstein,Carmen Boullosa,Kamala Suraiyya,Langston Hughes,Adrienne Rich,Nikki Giovanni" #1 author_names = authors.split(",") print("1:",author_names) #2 #personnal way found by join() the author_names list into a string and by split() this string into another list, basically applying two split, first for "," and second for spaces. Then I defined a function that is appending the odd numbers into the new list. separator = " " author_names_1 = separator.join(author_names) author_names_2 = author_names_1.split() def last_names(lst): lst2=[] for i in range(len(lst)): if i % 2 != 0: lst2.append(lst[i]) return lst2 author_last_names = last_names(author_names_2) print("2:", author_last_names) print("---") print("---") #Satisifed by my method I went to see the solution to find out of course that my method was working but unsatisfied I decided to appropriate the solution, here is a couple of bonuses :) #bonus first names first =[] for name in author_names: first.append(name.split()[0]) print("first names:",first) print("---") #bonus last names last =[] for name in author_names: last.append(name.split()[-1]) print("last names:",last)

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

# Initialise an empty list author_last_names = [] # Iterates temp variable through the list for name in author_names: # Splits each string and concatenates the new lists to empty list, creating a series of nested lists author_last_names.append(name.split()) # ['Audre Lorde'] = [['Audre', 'Lorde']] # As we only want the last_name we select the last element using negative indexing author_last_names.append(name.split()[-1]) print(author_last_names)

I hope this helps

Hi, I solved this using list comprehension with the original string authors.

authors = "Audre Lorde,Gabriela Mistral,Jean Toomer,An Qi,Walt Whitman,Shel Silverstein,Carmen Boullosa,Kamala Suraiyya,Langston Hughes,Adrienne Rich,Nikki Giovanni" author_last_names = [author.split()[-1] for author in authors.split(",")] print(author_last_names)

Is there an alternative method to do this?

feel like giving up. I would have never figured this ou

1 Like