Poetry project lesson

Your boss at the Poetry organization sent over a bunch of author names that he wants you to prepare for importing into the database. Annoyingly, he sent them over as a long string with the names separated by commas.

Using .split() and the provided string, create a list called author_names containing each individual author name as it’s own string.

Checkpoint 2 Passed

2.

Great work, but now it turns out they didn’t want poet’s first names (why didn’t they just say that the first time!?)

Create another list called author_last_names that only contains the last names of the poets in the provided string.

this how i went about mine.

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(‘,’)

print(author_names)

names = [name.split() for name in author_names]

print(names)

this is for the second one.

author_last_names = [last_name[-1] for last_name in names]

print(author_last_names)

Do you have a specific question about the lesson?

i would be glade to contact you for help as and when the need arises.

thanks so much

Can anyone explain the logic of this to me. The answer worked, however, I still have trouble understanding how or why it worked.

We start by splitting the string into a new list called “names.”

Then we take that new list, and split it into yet another new list called “author_last_names.”

I can see that far, maybe it’s the specific way that the loop is formatted that’s confusing me and I’m having trouble seeing the logic of how both loops work and exactly what the loops are doing.

Thanks!

Tracing back to the beginning, we are given a long string, with values separated by a comma (‘,’). Since it is a, <class 'str'> object, we know that it has a, .split() method, which we can apply to build a list from the comma separated values (full names).

authors = ".., .., .."
author_names = authors.split(',')

The items in the list are all strings, so they too have a, .split() method which we will use to split the names into individual lists.

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

Above we utilize a list comprehension which we’ll assume you understand.

[expression on item for item in iterable]

Our comprehension’s expression is, name.split()[-1] since that operation will yield only the last value in the individual list. We get the list we are after.

Below is the complete code run in the interactive Shell:

>>> 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(',')
>>> last_names = [name.split()[-1] for name in author_names]
>>> last_names
['Lorde', 'Mistral', 'Toomer', 'Qi', 'Whitman', 'Silverstein', 'Boullosa', 'Suraiyya', 'Hughes', 'Rich', 'Giovanni']
>>> 

Oh…

Haha, now I get it…I think, does this make sense?

last_names = [names.split[-1] for name in author_names]

this is splitting the -1 index from the list author_names, which is storing the item at that index, the last name, into a new list that we just defined.

I think I was getting confused because I tried it without the list comprehension, and was getting thrown errors for trying to split a list, but what I wasn’t realizing is that the items in said list, are strings, which is how and why this works?

.split() is a function, remember. It must be invoked with, () at the very least (it splits by default on space characters). The return from that is a list, which supports subscripting, hence, recognition of the [-1].

1 Like

Making more sense now.

Thanks!

1 Like

Extra Study

This is strictly for off time when you start looking under the hood at dunder methods. That time is not now, but just as an example, let’s consider how we might determine if an object supports subscripts.

One approach leverages the attributes on an object, something all object have. We can query that object’s attributes and look for a method called, __getitem__().

The easiest way to check that is with the built-in, hasattr() function.

a = range(3, 91, 3)

We know that there are 30 values in that sequence. Can we access any value without iterating the complete range? Let’s find out:

>>> a = range(3, 91, 3)
>>> hasattr(a, '__getitem__')
    True
>>>

That tells us that a range object does support subscripts which tells us we can access any value in the sequence by its index.

>>> a.__getitem__(0)
    3
>>> a.__getitem__(29)
    90
>>>

Of course why would we write that when we can just use the subscript, instead…

>>> a[29]
    90
>>>

Under the hood, Python is translating our subscript to that method call for us. That is how dunder methods work. Behind the scenes. They are called ‘dunder’ because of the double underscore on each side of their name. We never expect to see them in a program unless it is a class definition that demonstrates them or defines a special method requiring one. You’ll learn about this way down the road so don’t be squirrelling down any rabbit holes, just yet. Soon enough the language will be less and less of a mystery. Plenty of time for that later.

2 Likes