How should I use `.split()` to remove commas between names?

I wrote the following code as an answer for the part 1:

author_names = authors.split(", ")

But this was not the expected solution and what was expected seems to be:

author_names = authors.split(",")

which results an unnatural space before the second and subsequent names.
Should this be reported as a bug?

In the context of the exercise this is not a bug since it has no effect on the desired outcome, namely, the last names. So long as that result is as expected the exercise will pass.

You do raise a meaningful observation, and point out a potential future problem to solve when parsing such lists. Let that be a bug in your conscience so you will pursue it, further.


  • Is it safe to assume that a comma-separated list will have space characters following every comma?
  • Should said spaces be there would we be better to strip them later?
  • When given a random list of names, what other characteristics would it be useful to investigate?


>>> authors = '''
Audre Lorde, William Carlos Williams, Gabriela Mistral,
Jean Toomer, An Qi, Walt Whitman, Shel Silverstein,
Carmen Boullosa, Kamala Suraiyya, Langston Hughes,
Gabriel García Márquez, Adrienne Rich, Nikki Giovanni
>>> print ([
  x.split()[-1] for x in authors.split(',')
['Lorde', 'Williams', 'Mistral', 'Toomer', 'Qi', 'Whitman', 'Silverstein', 'Boullosa', 'Suraiyya', 'Hughes', 'Márquez', 'Rich', 'Giovanni']

Thank you for your reply! I see, the former code I wrote can cause another problem if there is no space after a comma, and will be incomplete when there are multiple consecutive spaces after a comma. Using the method .strip() in the later exercise seems better way to strip such spaces.


sir, can you explain your code, it is difficult for newbie like me?

First, let’s look at the result. What did we produce? Next, what was the input? These are not rhetorical questions. They do have answers. Please go ahead and answer them, and we will continue…

This one line code is nice. I will also try to sum up my code in one line for next time whenever i will get this type of question.

1 Like

Your solution is very nice and concise…hope I’d be able to write such concise codes one day. Btw i use for loop and iterate to get the solution.

k = list()
for x in author_names:
  b = x.split()
author_last_names = k