How to select only last names?

If you look at the documentation for the pop method,

Remove the item at the given position in the list, and return it. If no index is specified, a.pop() removes and returns the last item in the list.

For example,

x = [1, 4, 6, 8, 34]
y = x.pop()

print(y) # 34
print(x) # [1, 4, 6, 8]

# The popped/removed element is returned by the pop method.
# The original array is mutated.

One line assuming you split author_names correctly

author_last_names = [name.split(' ')[-1] for name in author_names]

I used both .split() and .pop() in a function to accomplish this task, I took advantage of the returning property of .pop() to store the last name. This is the function:

def last_names(authors):
last_names_list =
for author in authors:
splitted_author = author.split(" ")
last_name = splitted_author.pop()
last_names_list.append(last_name)
return last_names_list

Hey man, thank you so much. This was quite helpful

1 Like

this was the first problem I ever got stuck while learning python. it was very confusing!