How can we reference the right index if all our lists are seperate?

I was confused approaching step 10 because step 9 asks us to assign poems, titles, and authors to separate lists. Anyone know how to use those new lists for step 10 using only string methods we’ve learned up to this point?

So we’re clear, all three lists having corresponding indices. That is, the title at titles[0] corresponds with poets[0] and with dates[0]. This means we can iterate over one list, and use the index to access the other two.

for i in range(len(titles)):

where i is the index of the current iteration.

title, poet, date = titles[i], poets[i], dates[i]

Now just plug those variables into the format arguments and Bob’s your uncle.

8 Likes

Awesome, you’ve made it clear. I was thinking iterations but I’ve taken a few weeks off of working through this python course (which I’m loving). Thanks!

2 Likes

Hi, after practicing this review, I wanna print out the last thing which is “The poem TITLE was published by POET in DATE”

However, when I tried to print out the output, it showed nothing.
Here is my code:

titles =
poets =
dates =
for j in highlighted_poems_details:
titles.append(j[0])
poets.append(j[1])
dates.append(j[2])

def everthing_combine(titles, poets, dates):
for title in titles:
for poet in poets:
for date in dates:
return “The poem {} was published by {} in {}.”.format(title, poet, date)

Thank you very much

When that does print it will be reams of lines owing to the triple nested loops. This is one for the boneyard. Scrap it and think it through again.

Hint:

All the arrays are the same length. Establish a range of that size and iterate it for the index, then use it on all three objects in each iteration.

2 Likes

im also confused in the last one:

for poem in highlighted_poems_details:
titles.append(poem[0])
poets.append(poem[1])
dates.append(poem[2])

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

im actually confused alwaysi found this [0], [1] and then titles[i], poets[i], dates[i], i know its the index but when to know i actually have to used them?

and then to guess i needed two for loops, :frowning: this is hard

Here’s my solution. My final for loop in particular feels clunky and brute forced. Any suggestions for cleaner code?

for record in highlighted_poems_details:
  titles.append(record[0])
  poets.append(record[1])
  dates.append(record[2])

counter = 0
for i in range(len(titles)):
  print("The poem " + titles[counter] + " was published by " + poets[counter] + "in " + dates[counter])
  counter = counter + 1
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 = []

for poem in highlighted_poems_list:
  temp = poem.strip(' ')
  highlighted_poems_stripped.append(temp)

#print(highlighted_poems_stripped)

highlighted_poems_details = []

for record in highlighted_poems_stripped:
  temp = record.split(':')
  highlighted_poems_details.append(temp)

titles = []
poets = []
dates = []

for record in highlighted_poems_details:
  titles.append(record[0])
  poets.append(record[1])
  dates.append(record[2])

counter = 0
for i in range(len(titles)):
  print("The poem " + titles[counter] + " was published by " + poets[counter] + "in " + dates[counter])
  counter = counter + 1
2 Likes

Can’t understand why this isn’t working for item #10.

The output produced is

The poem Dreamwood was published by 1987 in Adrienne Rich

My expectation was that it would iterate over the total range of the list Titles and that would output the other list variables at the respective range values. I printed the range values of the

Here is the relevant part of the code:

def poem_description(title, poet, date):
  poem_desc = ''
  for i in range(len(title)): 
    poem_desc = "The poem {} was published by {} in {}".format(title[i],poet[i],date[i])
  return poem_desc

print(poem_description(titles,dates,poets))

Here is all the 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)

highlighted_poems_list  = highlighted_poems.split(',')

print(highlighted_poems_list)

highlighted_poems_stripped = []

for x in highlighted_poems_list:
  highlighted_poems_stripped.append(x.strip())

print(highlighted_poems_stripped)

highlighted_poems_details = []

for poem in highlighted_poems_stripped:
  highlighted_poems_details.append(poem.split(':'))
  
print(highlighted_poems_details)

titles = []
poets = []
dates = []

for i in highlighted_poems_details:
  dates.append(i [-1])
  
for i in highlighted_poems_details:
  titles.append(i [0])

for i in highlighted_poems_details:
  poets.append(i [1])

print(range(len(titles)))
print(range(len(poets)))
print(range(len(dates)))


def poem_description(title, poet, date):
  poem_desc = ''
  for i in range(len(titles)): 
    poem_desc = "The poem {} was published by {} in {}".format(title[i],poet[i],date[i])
  return poem_desc

print(poem_description(titles,dates,poets))

TIA

def poem_description(title, poet, date):
  poem_desc = ''
  for i in range(len(titles)): 
    poem_desc = "The poem {} was published by {} in {}".format(title[i],poet[i],date[i])
  return poem_desc

Every time around the loop, the variable, poem_desc is overwritten, so you only return the final instance of the loop. Collect them, somehow, perhaps in a list.

1 Like

Thanks. The below worked:

def poem_description(title, poet, date):
  poem_desc = []
  for i in range(len(titles)):
      poem_desc.append("The poem {} was published by {} in {}".format(title[i],poet[i],date[i]))
  return poem_desc

print(poem_description(titles,dates,poets))
1 Like

While there are few ways for sure to solve this last piece of the problem, I found below code doing just the right job - and took me some effort too to finally understand indexing.
You basically take the number of all titles, then use it as an index to your format inputs:

for x in range(len(titles)):
print(“The poem {a} was published by {b} in {c}.”.format(a=titles,b=poets,c=dates))

Was it a bug that I got a check-mark on this?

#print(highlighted_poems)

highlighted_poems_list = highlighted_poems.split(',')
#print(highlighted_poems_list)

highlighted_poems_stripped = []
for poem in highlighted_poems_list:
  x = poem.strip()
  highlighted_poems_stripped.append(x)
#print(highlighted_poems_stripped)

highlighted_poems_details = []
for poems in highlighted_poems_stripped:
  x = poems.split(':')
  highlighted_poems_details.append(x)
#print(highlighted_poems_details)

titles = []
poets = []
dates = []
for details in highlighted_poems_details:
  titles.append(details[0])
  poets.append(details[1])
  dates.append(details[2])

for poems in highlighted_poems_details:
  for i in range(len(highlighted_poems_details)):
    print("The poem "+titles[i]+" was published by "+poets[i]+" in "+dates[i]".")

Upon running this code, I get:

File "script.py", line 30
    print("The poem "+titles[i]+" was published by "+poets[i]+" in "+dates[i]".")
                                                                               ^
SyntaxError: invalid syntax

And the lesson clears.

EDIT: I know I’m missing a “+” and that it won’t print correctly, solved this after the fact, but I wanted to submit this because I think it’s weird that the course cleared me. You’ll find what I went with at the last step below.

for i in range(len(titles)):
  print("The poem {title} was published by {poets} in {dates}.".format(title=titles[i], poets=poets[i], dates=dates[i]))

This was my codes outcome, it worked, but i feel it can be cleaner too

titles = []
poets = []
dates = []
for info in highlighted_poems_details:
  titles.append(info[0])
  poets.append(info[1])
  dates.append(info[2]) 

def poem_discriotion(titles, poets, dates):
  poem_des = []
  for i in range(len(highlighted_poems_details)):
    poem_des.append("The poem {} was published by {} in {}.".format(titles[i], poets[i], dates[i]))
  return poem_des

poem_join = poem_discriotion(titles, poets, dates)
poem_join = "\n".join(poem_join)
print(poem_join)

Strings Review

can be simplified to,

for a, b, c in highlighted_poems_details:
  titles.append(a)
  poets.append(b)
  dates.append(c)

Notice how we are able to unpack each three element sequence in the for loop?

Since all lists correspond with each other, we can enumerate to get one value, and use its index on the other two.

print ('-' * 32)
for i, x in enumerate(titles):
  print ("The poem {title} was published by {poet} in {date}.\
".format(title=x, poet=poets[i], date=dates[i]))

Not like any of the above really makes any difference, other than being another take on things.

1 Like

Thank you, this is much better indeed

1 Like

Someone please re-explain the difference between:

for n in titles:

&

for n in range(len(titles)):

I just spent too long editing that last action of code to find that I only needed to change from the former to the latter.
Aren’t they both iterating over the length of titles?

Yes, except the first one is iterating the values in a read-only fashion (cannot modify values); the second iterates over the range to extract indices. n is an integer, not a value, per se. This gives us access to the values so they can be mutated.

1 Like

You’re the man. Got it.

1 Like