FAQ: String Methods - Splitting Strings II

A post was merged into an existing topic: How to select only last names?

A post was merged into an existing topic: How to select only last names?

2 posts were split to a new topic: How can I improve my code?

2 posts were split to a new topic: My solution

A post was merged into an existing topic: How to select only last names?

3 posts were split to a new topic: How should I use .split() to remove commas between names?

In the 1st exercise, I think that you should allow bellow to work, because you actually want to remove the space between authors

author_names = authors.split(", ")
4 Likes

I agree!
That’s exactly what I did, and was surprised when it was not accepted as right.
I tested with print and it seemed perfect.
I struggled to figure out what the expected goal was.

1 Like

Uhm. Have you looked at the string though? What delimits the authors? It’s not ", "

authors = "Audre Lorde,Gabriela Mistral,Jean Toomer,An Qi,Walt Whitman,Shel Silverstein,Carmen Boullosa,Kamala Suraiyya,Langston Hughes,Adrienne Rich,Nikki Giovanni"

I don’t know how and why, but I checked again and my saved solution looks like this:

authors = "Audre Lorde, William Carlos Williams, 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)

def grab_last_name(name_list):
** last_name_list = **
** for name in author_names:**
** last_name_list.append(name.split()[-1])**
** return last_name_list**

author_last_names = grab_last_name(author_names)
print(author_last_names)

So, ", " delimits the authors.

Interesting thing is that now when I reset this exercise I get a list of authors delimited only by “,” as you posted.

But then, I tried to paste the first part of my code (including authors list whit blank space) after reset and I still get:
“Value for author_names did not match the expected value.”
List looks perfect:
[‘Audre Lorde’, ‘William Carlos Williams’, ‘Gabriela Mistral’, ‘Jean Toomer’, ‘An Qi’, ‘Walt Whitman’, ‘Shel Silverstein’, ‘Carmen Boullosa’, ‘Kamala Suraiyya’, ‘Langston Hughes’, ‘Adrienne Rich’, ‘Nikki Giovanni’]
Then I remove blank space after “,” and my code is accepted as correct eventhough list looks wrong (whit blank space before names in the list:
[‘Audre Lorde’, ’ William Carlos Williams’, ’ Gabriela Mistral’, ’ Jean Toomer’, ’ An Qi’, ’ Walt Whitman’, ’ Shel Silverstein’, ’ Carmen Boullosa’, ’ Kamala Suraiyya’, ’ Langston Hughes’, ’ Adrienne Rich’, ’ Nikki Giovanni’]

Anyway, I’m quite sure that the list for some reason looked different, and [brunpierreantoine] had the same problem.

I’ve found another discussion on the same problem:

1 Like

Hi,

The code below printed a list of surnames as was required, but the code editor did not accept the solution. This is what was printed out:
[‘Lorde’, ‘Williams’, ‘Mistral’, ‘Toomer’, ‘Qi’, ‘Whitman’, ‘Silverstein’, ‘Boullosa’, ‘Suraiyya’, ‘Hughes’, ‘Rich’, ‘Giovanni’]

#And this was the code:

def author_last_names():
  surnames = []
  for index in range(0, len(author_names)):
    split = author_names[index].split()
    surnames.append(split[-1])
  print(surnames)
 
author_last_names()

What have I done wrong?

Double check the requirements. Does it ask you to print values out or does it mention something else?

Hi,

If the string contains only the delimiter as its content, like seen below, why does ‘split’ return two elements list?

test = “*”

print(test.split(’*’))

Result: [’’, ‘’]

1 Like

Because its counterpart, str.join() needs that pattern in order to reconstruct the string.

>>> test = "*"
>>> test_list = test.split('*')
>>> test_list
['', '']
>>> test_str = '*'.join(test_list)
>>> test_str
'*'
>>> 

Now let’s tweak the list and remove the quotes…

>>> '*'.join([''])
''
>>> '*'.join([])
''
>>> 
2 Likes

Thank you. The ‘join’ examples that are shared makes me wonder about the internal representation of empty list vs that of list with one element that is an empty string! Why are both producing same result?

separator.join(sequence) is concatenating the sequence of strings with separator between them (where separator is a string). A few examples might help-

list_letters = ['a', 'b', 'c']  
''.join(list_letters)  # Out: 'abc'
' '.join(list_letters) # Out: 'a b c'
'*'.join(list_letters)  # Out: 'a*b*c'
# effectively... 'a' + '*' + 'b' + '*' + 'c'

Note that with three values we only have two separators. So if our sequence to iterate was only one item long?

'test'.join(['b'])  # Out: 'b'
'test'.join([''])  # Out: ''

We just get the sequence without any seprators. This includes the single empty string. With a completely empty sequence, e.g. an empty list [] we just get an empty string returned every time''.

''.join([])  # Out: ''
'test'.join([])  # Out: ''

So I have two different lists. The only difference is the index. I would assume [-1] and [-1:] would be the same thing but the output tells otherwise (Extra brackets which did not count as a solution). Why is this?

authors = "Audre Lorde, William Carlos Williams, 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]

author_last_names2 = [name.split()[-1:] for name in author_names]

print(author_last_names)
print(author_last_names2)

Output:

['Lorde', 'Williams', 'Mistral', 'Toomer', 'Qi', 'Whitman', 'Silverstein', 'Boullosa', 'Suraiyya', 'Hughes', 'Rich', 'Giovanni']
[['Lorde'], ['Williams'], ['Mistral'], ['Toomer'], ['Qi'], ['Whitman'], ['Silverstein'], ['Boullosa'], ['Suraiyya'], ['Hughes'], ['Rich'], ['Giovanni']]

Thanks in advance!

accessing an element by index ([-1]) will give the element/value from the list

using list slicing ([-1:]) will give a list. That is why its called list slicing, you want a piece of the list.

In the context of this exercise, if we concatenate using += instead of using .append(), why does it create a list of the characters within each surname instead of complete surnames?

e.g.

author_last_names = []
for names in author_names:
    author_last_names += names.split()[-1]
print(author_last_names)

#Prints ['L', 'o', 'r', 'd', 'e', 'W', 'i', 'l', 'l', 'i', 'a', 'm', 's', ... and so on]

Is it because strings cannot be concatenated onto lists, so it converts the string into a list first?
i.e. list('string') -> ['s', 't', 'r', 'i', 'n', 'g']