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

You are building poem_desc, and writing over it each time through the loop and only returning it once. Add the print function into the loop and that will solve the issue. Also, if you are going to use a function for this, just remember to have the sequence of your parameters in order. You have dates and poets switched when you call the function. Cheers!

for title, poet, dat in titles, poets, dates:
print(“The poem {0} was published by {1} in {2}”.format(title, poet, dat))

Please why doesn’t this work i keep getting too many values to unpack

Would that work better if the in object was a single data point?

for a, b, c in d:

fixed it so my problem was i had to zip the in object so it was like a tuple

1 Like

8 posts were split to a new topic: Is this correct?

Oh but “highlighted_poems_details” is the same as titles in the second loop btw. I just chose titles because it’s easier to visualize with shorter lists of 1/3 elements at a time. I’m pretty sure they tell you to use “highlighted_poems_details” as its a better path for lists that are uneven.

Nice one! It’s not so brute force but actually, in your last loop, you could do without your counter variable.
You set counter = 0 and then enter the for loop by setting i in range(len(titles)), meaning that the i value will range from 0 to the length of titles. Basically i is always equal to counter throughout your loop.

If you write:

counter = 0
for i in range(len(titles)):
  print(i == counter)  # prints True if i = counter, or False if i ≠ counter
  print("The poem " + titles[counter] + " was published by " + poets[counter] + "in " + dates[counter])
  counter = counter + 1

You’ll get a True line at each loop iteration. So you can use i instead of setting this counter variable.

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

Finally, your print statement could also be improved by using format() as such:

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

But you had the right thinking throughout your logic!

I quickly scrolled through the answers so I might have not seen it in the thread but my solution was this:

for details in highlighted_poems_details:
  print("The poem {title} was published by {poet} in {date}.".format(title = details[0], poet = details[1], date = details[2]))

My main issue is trying to understand better why we use range(0,len(highlighted_poems_details)). I used print(highlighted_poems_details) just to get a visual at what it looks like then trying to comprehend how range and length come into play here. I sort of understand it but I wouldn’t have thought to use that for the solution. I had to look at the solution for 10 to get it to work.

Also, not really that important, but why are people using range(0,len(titles))? The instructions do not ask to iterate through just ‘titles’ so its a little confusing to see here in the thread that people are writing it in their code.

range() takes 3 arguments. start, stop, step(this one is optional).
So, it starts at 0 index and goes the length of that list.

Did you scroll up in this very thread and see this?

And, the docs on built-in functions range() and len()

I know how range() and len() function. I wasn’t sure why we had to use it in the solution for #10.

I did see that previous post and it also doesn’t help me understand we we need to use range() and len() for the solution.
I need a simpler explanation that follows along with the details from the lesson.

Again: for i in range(len(titles)) was not something that was needed or included in the lesson. I don’t understand why this is being explained…

This is the Review part of the lesson, right? (Unless I’m looking at the wrong thing).
I don’t see where ppl are iterating through just the titles list. the question asks one to iterate through highlighted_poems_details.

So, in the previous item, 9, you did something like this, right?

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

>>>which produces this:
['Afterimages: Audre Lorde:1997', 'The Shadow:William Carlos Williams:1915', 'Ecstasy:Gabriela Mistral:1925'....]

And in q10, they’re asking you to: " Finally, write a for loop that uses .format() to print out the following string for each poem: “The poem TITLE was published by POET in DATE”

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

>>>which results in:
The poem Afterimages was published by Audre Lorde in 1997
The poem The Shadow was published by William Carlos Williams in 1915
The poem Ecstasy was published by Gabriela Mistral in 1925...etc

In that, you’re iterating through each item in highlighted_poems_details and pulling out the appropriate information for title, published by, and date.

Yes, this is the Review part you are looking at the correct thing. And that’s why I mentioned some people are posting in this thread about iterating through just titles, which was not the ask in the review.

My code looks like:
for item in highlighted_poems_details:
titles.append(item[0])
poets.append(item[1])
dates.append(item[2])

print(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]))


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

is what i’m asking about and trying to understand why we have to use range and len here. I think I need to visually see something tangible to understand how it works instead of it just being explained. I struggle to comprehend when and why we should use range() and len() together to find the solution.

If it helps, draw it out (I’ve done this myself in order to understand concepts).
for i in range(0,len(highlighted_poems_details)

for each item in the range of items in the list, starting at index 0.
You use range() to iterate through a list object.

len() computes the length of the list highlighted_poems_details
if you do a

print(len(highlighted_poems_details))

>>11

if you print that list:

print(highlighted_poems_details)
#you get lists inside a list, or a list of lists.
[['Afterimages', 'Audre Lorde', '1997'], ['The Shadow', 
'William Carlos Williams', '1915'], 
['Ecstasy', 'Gabriela Mistral', '1925']...]

When you combine range(0,(len...)), you’re iterating through the entire length of the list, highlight_poems_details starting at index 0, which is the first sublist, Audre Lorde’s poem, etc. until it reaches the end of the list, the poem by Adrienne Rich.

devs could’ve thought of this:
you don’t actually need 3 separate lists(titles, poets, dates)
you can print:

for i in highlighted_poems_details:
  print("The poem {title} was published by {poet} in {date}.".format(title = i[0], poet = i[1], date = i[2]))

you know where highlighted_poems_details came from:

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 = []
for i in highlighted_poems_list:
  highlighted_poems_stripped.append(i.strip())
highlighted_poems_details = []
for i in highlighted_poems_stripped:
  highlighted_poems_details.append(i.split(":"))

it does the exact same thing, but leaves out 7 extra lines:

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

looking at this you can even see the instances of ‘i[0]’, ‘i[1]’, and ‘i[2]’
i saw earlier in the thread someone with:

for a, b, c in highlighted_poems_details:
  print("The poem {title} was published by {poet} in {date}.".format(title = a, poet = b, date = c)

or something like that.
makes it look a lot cleaner without the ‘i[0]’ stuff.

i started to try adding a custom function, thought better of it later on though

yea, when is a keyword there, if computers worked at less than light speed that will take forever to finish

for question no.10, is my code okay or is it better to use the solutions one? i need some sort of code review

my code:

for detail in highlighted_poems_details:
  print("The poem {title} was published by {poet} in {date}"
        .format(title=detail[0], poet=detail[1], date=detail[2]))

solution:

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

There is nothing wrong with your code, though I would say that using keywords is overkill unless your want the order in the output to be changed up some how. Format keywords are not positional, meaning their order doesn’t matter.

In the solution code it looks like the author has already created the three lists suggested so there are four lists of the same length. Any one of them could have been referred in the signature line.

for i in range(len(titles)):

would give the same range.

As this is an old lesson, it may have predated the release of the new f-string, which takes variables and expressions directly:

for title, poet, date in highlighted_poems_details:
    print (f"The poem {title} was published by {poet} in {date}")
#          ^
#          |
#    f-string operator

As for the solution, if that is what is expected, meaning your solution was not, then we should go along with it. If any solution is accepted, assuming it gives the correct output, then use what you feel most comfortable with.

3 Likes

thank you very much sirr

1 Like