FAQ: String Methods - Review

FTR: Where I used the term ‘directive’, it should be, ‘specifier’.

Hello there,

For this exercise, I’m getting a “list index out of range” error for step 7 (the output of highlighted_poem_details, where it wants a list of lists of the poems, split by the colon)

(Exercise in question) https://www.codecademy.com/journeys/data-scientist-ml/paths/dsmlcj-22-data-science-foundations/tracks/dsmlcj-22-python-fundamentals-for-data-science-part-ii/modules/dsf-python-strings-856c707b-20b8-42db-ba5f-cdf8fea8c232/lessons/string-methods/exercises/review-ii

However, unless I’m mistaken, I’m getting the desired result.

Attached is a screenshot and below is a copy of my code and the output.

Offending snippet of code (full code will be copied below)

def split_poem_list(item_list):
  split_item_list = []
  for item in item_list:
    split_item = item.split(':')
    split_item_list.append(split_item)
  return split_item_list

highlighted_poem_details = split_poem_list(highlighted_poems_stripped)
print(highlighted_poem_details)

offending Output:

[[‘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’]]

Full Code

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"

print('highlighted poems')
print(highlighted_poems)
print('######')

highlighted_poems_list = highlighted_poems.split(',')
print('highlighted poems list')
print(highlighted_poems_list)
print('######')

def strip_white_space_on_list(item_list):
  stripped_items = []
  for item in item_list:
    stripped_item = item.strip()
    stripped_items.append(stripped_item)
  return stripped_items

highlighted_poems_stripped = strip_white_space_on_list(highlighted_poems_list)
print('highlighted_poems_stripped')
print(highlighted_poems_stripped)
print('######')

highlighted_poems_details = []

def split_poem_list(item_list):
  split_item_list = []
  for item in item_list:
    split_item = item.split(':')
    split_item_list.append(split_item)
  return split_item_list

highlighted_poem_details = split_poem_list(highlighted_poems_stripped)
print('highlighted_poem_details')
print(highlighted_poem_details)
print('######') 


Full output

Output:
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
######3
highlighted poems list
[‘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_stripped
[‘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_poem_details
[[‘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’]]

According to your screenshot, Step 7 specifies:

  1. Iterate through highlighted_poems_stripped and split each string around the : characters and append the new lists into highlighted_poems_details.

You created the empty list (Step 6) highlighted_poems_details correctly.

But, you are assigning the result of Step 7 to a list named highlighted_poem_details.
The variable name should be ..._poems_... instead of ..._poem_...

what should we do here if there were some missing values in the highlighted_poems list? for example let us assume dates of some poems or authors of some poems are missing. how we handle this situation to do instructions?

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" print(highlighted_poems) highlighted_poems_list = highlighted_poems.split(',') print(highlighted_poems_list) highlighted_poems_stripped = [] def stripping(x): strip_list = [] for y in x: strip_list.append(y.strip()) return strip_list highlighted_poems_stripped = stripping(highlighted_poems_list) print(highlighted_poems_stripped) highlighted_poems_details = [] def detailer(a): detail_list = [] for b in a: b_split = b.split(":") detail_list.append(b_split) return detail_list

This is the code I have at step 7, and the output looks OK, but I get an error on ‘list index out of range’. Could I get insight on what this means and how to move to step 8?

Step 7 specifies:

  1. Iterate through highlighted_poems_stripped and split each string around the : characters and append the new lists into highlighted_poems_details.

Where are you appending the lists to highlighted_poems_details ?

You are returning a new list of lists, but after Step 7, highlighted_poems_details is still empty.

This is the last bit of the code, thought it was in my paste.

highlighted_poem_details = detailer(highlighted_poems_stripped)

print(highlighted_poem_details)

[[‘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’]]

It generates the below output with the print, and it looks OK, but with the error of “list index out of range” it won’t let me go to 8.

EDIT: realized I had the details variable slightly off. sorted out, thanks