FAQ: String Methods - Review

Oh sorry!

1 Like

Are you on step 8? Please post the raw code for that step (not an image).

Step 9.
Here is the code. When I run it the print outputs give the correct lists for each individual list, but I get an error that it didn’t produce the expected values so I’m confused. :slight_smile:

highlighted_poems = “Afterimages:Audre Lorde:1997, The Shadow:William Carlos Williams:1915, Ecstasy:Gabriela Mistral:1925, Georgia Dusk:Jean Toomer:1923, Parting Before Daybreak:An Qi:2014, The Untold Want:Walt Whitman:1871, Mr. Grumpledump’s Song:Shel Silverstein:2004, Angel Sound Mexico City:Carmen Boullosa:2013, In Love:Kamala Suraiyya:1965, Dream Variations:Langston Hughes:1994, Dreamwood:Adrienne Rich:1987”


highlighted_poems_list = highlighted_poems.split(“,”)


highlighted_poems_stripped = [poem.strip() for poem in highlighted_poems_list]


highlighted_poems_details = [poem.split(“:”) for poem in highlighted_poems_stripped]

titles = [detail[0::3] for detail in highlighted_poems_details]

poets = [detail[1::3] for detail in highlighted_poems_details]

dates = [detail[2::3] for detail in highlighted_poems_details]




It looks like the slicing is the issue. You have lists of lists, instead of three, one dimension lists.

1 Like

It seems to work though, this is what I get printing the 3 lists out:

[[‘Afterimages’], [‘The Shadow’], [‘Ecstasy’], [‘Georgia Dusk’], [‘Parting Before Daybreak’], [‘The Untold Want’], [“Mr. Grumpledump’s Song”], [‘Angel Sound Mexico City’], [‘In Love’], [‘Dream Variations’], [‘Dreamwood’]]
[[‘Audre Lorde’], [‘William Carlos Williams’], [‘Gabriela Mistral’], [‘Jean Toomer’], [‘An Qi’], [‘Walt Whitman’], [‘Shel Silverstein’], [‘Carmen Boullosa’], [‘Kamala Suraiyya’], [‘Langston Hughes’], [‘Adrienne Rich’]]
[[‘1997’], [‘1915’], [‘1925’], [‘1923’], [‘2014’], [‘1871’], [‘2004’], [‘2013’], [‘1965’], [‘1994’], [‘1987’]]

OH I get what you are saying, they are lists of lists instead of lists of strings! THANK YOU haha I knew there was something I was just not seeing. :slight_smile:

I thought I was being so clever making those code slices too :joy:

1 Like

There is your list of lists. It should look like,

['Afterimages', 'The Shadow', 'Ecstasy', 'Georgia Dusk', 'Parting Before Daybreak', 'The Untold Want', "Mr. Grumpledump's Song", 'Angel Sound Mexico City', 'In Love', 'Dream Variations', 'Dreamwood']
1 Like

Thanks a bunch! :slight_smile: Appreciate your help and time!

1 Like

I’m on the last part of this review and I really don’t understand the final step. The use of range() and len() in this context doesn’t make sense to me. It doesn’t seem like we’re doing anything with the length or range of highlighted_poem_details so why does that come into play.

If anyone can offer some insight that’d be greatly appreciated

This is the codeacademy supplied solution
for poem in highlighted_poems_details:

for i in range(0,len(highlighted_poems_details)):
print(‘The poem {} was published by {} in {}’.format(titles[i], poets[i], dates[i]))

Since a range object is a sequence of integers, and it starts at zero by default, it is an excellent tool for produces indices in order from 0 to n, when n is the length of the list (range excludes the last value).

We are not iterating over the list as much as iterating over the range and accessing the list at each index.

n = len(highlighted_poems_details)

r = range(n)

for i in r:
    # i is an index for the three corresponding lists

Okay, so what you’re saying is that this is a method of going through and making sure that at each index we add the correct input from the three lists: titles, poets and dates?

1 Like

Essentially, yes. We use the range to give us indices, and apply each to the three respective lists which all correspond with each other.

1 Like