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.

1 Like