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.


Consider,

  • 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?

Eg.

>>> 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']
>>> 
25 Likes

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.

2 Likes

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()
  k.append(b[-1])
print(k)
author_last_names = k 
3 Likes

Talk about conciseness. I like your code, not as dirty as mine =P
Even went to the trouble of learning how to re.split, since I had problems splitting the space from the already separated no-comma list.

import re x= re.split(',| ', authors) def separate_x(x): y = [] for i in range(len(x)): if i%2 != 0: y.append(x[i]) return y author_last_names = separate_x(x) print(author_last_names)

We produced a list with last names. Input was a string with “first_name last_name” separated by a comma and then a space.

I’m trying to understand your code but have a hard time to do so. If you could please enlighten us good sir!

authors.split(',')

produces a list of strings from a string of comma separated strings. That list consists of first and last names, with sometimes a middle. We want to extract only the last names so iterating over the above object gives us one full name at a time, now as a list from which we can access the last element and print that.

1 Like