FAQ: String Methods - Splitting Strings II

I think the biggest difference is that print does not return the actual variable values. Your author_last_names variable would contain the None object instead of actual author names.

Is it the same output? Double check. What about those with three provided names?

1 Like

Heh, it was that simple. Next time I’ll double check this… Thanks for helping me this fast and efficiently!

Hi I have a quick question,

uthor_names = authors.split(',')
print(author_names)
print(type(author_names))

def remove_last_name():
  authors_split = []
  for i in author_names:
    #authors_split.append(i.split())
    bob = i.split()
    authors_split.extend(bob)

  print(len(authors_split))
  authors_last_names = [authors_split[i] for i in range(len(authors_split)) if i % 2 != 0]
  print(authors_last_names)
  return authors_last_names
  #for lastname in author_names

author_last_names = remove_last_name()

In my list comprehension:

authors_last_names = [authors_split[i] for i in range(len(authors_split) - 1) if i % 2 != 0]

Returns all last names except the final name on the list.

authors_last_names = [authors_split[i] for i in range(len(authors_split)) if i % 2 != 0]

Returns all including the last name. I’m confused because I thought the due to list indexes starting at 0 meant that you had to subtract one from the length for it to work.

this:

[1, 2, 3][-1]

means we get the last value from the list (using negative indexes)

doing -1 from range, you will just not give you the last element of the list

We use split() method to split a string into a list, can we use the same method to split a list?

In this case we can know that we cannot use split() function with a list.

However, what happened when we want to separate the element within a list?

We use split_name = name.split() to split the long list into 2 element list with the first and last name at the one list. Should we consider that we split the name (an element of the author_names list), or (a list within the author_names list)? Why we can split the name but not author_names?

Because the author name is a str type object, and has a split() method. Splitting gives us a list from a string; joining gives us a str from a list.

1 Like

Thank you so much for replying!
So is that mean an element in a list is not necessary a list type? An element in a list can have any type?

1 Like

Precisely. We can even mix types in a list.

1 Like

Thank you! That is really helpful!

1 Like

Hey guys, how are you?

Here’s the correct code for the 4/13 exercise:

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

#This is the first exercise
author_names = authors.split(",")
print(author_names)

#This is the second exercise
author_last_names =

for name in author_names:

author_last_names.append(name.split()[1])

print(author_last_names)

Can someone tell me why this code below doesn’t work? I tried this first:

author_last_names =

for name in author_names:

name.split()

author_last_names.append(name[1])

print(author_last_names

Thanks!!

.split() returns the result, you do nothing with the returned result, you would need to capture the return value:

returned_result = name.split()

which you then could use

1 Like

Thank you very much! It worked perfectly!

Here’s a possible solution and explanation to part 2 of the lesson below:

author_last_names = []
for i in range(len(author_names)):
  split_element = author_names[i].split()
  author_last_names.append(split_element[1])

Line 1: Create an empty list and store it in a variable with the intent of appending things to it.

Line 2: Start a for loop that will help us iterate through the splitted list via range() by accessing each index. Each index accesses a string in the splitted list (First & Last Name).

Line 3: Split each indexed element and store it in a variable called split_element, one at a time. For instance, the first loop should result in storing ["Audre", "Lorde"] in the split_element variable. Conveniently, the last name, Lorde, is now in a string of its own. This makes it really simple to append to the empty list in line 1.

Line 4: Append the last element of the split_element variable to author_last_names. Each loop, 11 in total, will run Lines 3 & 4. This means that the value of split_element variable will be replaced with each successive loop, and that a new last name will be appended.

I hope this helps. I’m sure there are better ways to solve this problem but this is what I could come up with.

tgrtim gave you the clue you needed, but you inspired me to make my own code much shorter:

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

I came up with the following:

authors = "Audre Lorde,Gabriela Mistral,Jean Toomer,An Qi,Walt Whitman,Shel Silverstein,Carmen Boullosa,Kamala Suraiyya,Langston Hughes,Adrienne Rich,Nikki Giovanni"
author_names = authors.split(',')
s = [x.split() for x in author_names]
author_last_names = [y for (x,y) in s]

Hi,

I thought this was easy, and the second question gets way too much harder for no reason and I got stuck. That’s very frustrating, so I came up looking at the solution, and I don’t even understand how it is working. Maybe I’m too dumb to keep continuing this python course idk

author_last_names = []
for name in author_names:
  author_last_names.append(name.split()[-1])
  
print(author_last_names)

I don’t get why there’s is a [-1], how does it do anything? I mean did I miss something really important in the course???
Python list and split/slice things is something I still can’t figure out, or even understand… this makes me very frustrated

1 Like

Python iterables can be accessed from either the left side, tarting at index zero and going up, or from the right side, starting at index negative one and going down.

-6 -5 -4 -3 -2 -1
"s  t  r  i  n  g'
 0  1  2  3  4  5

So, [-1] indicates the last element in a list, or the last letter in a string.

for i in range(1, 7):
    print ('string'[-i], end="")

    
gnirts

for the following code, is it possible to add the .upper() method somewhere in this line to made the result upper too, or do I need to make this a separate line and list?

author_last_names.append(name.split()[-1])

The value is a str so you can chain the method to that.

name.split()[-1].upper()
1 Like

Hi everyone,
Below is my solution for the 2nd task of the problem:

author_last_names = []
for i in range(len(author_names)):
  spl = author_names[i].split()
  author_last_names.append(spl[1])

print(author_last_names)

Is there any way I can improve this?
Any input is appreciated!

Thank you