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

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

text = (‘The poem {} was published by {} in {}’).format(titles[0], poets[0], dates[0])
print(text)

how i did it

Hello Everyone!

I would like to ask why is range(0,len(titles)) different from range(len(titles). I thought that in case the function takes a single input (being len(title)), it would still generate numbers starting at 0. However, if I do not include the 0, I only get the first line: “The poem Afterimages was published by Audre Lorde in 1997”. To have all lines in the result, not just the first, I needed to include 0 in the range function. Can somebody please explain why?
And that what is the difference between range(0, len(titles)) and range(len(titles)).

My function looks like the following after I included the 0 as the first argument for the range function:

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

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

I know the last part could be shorter, but I wanted to see something, so I preferred it this way.

return can only be used within a function. If you think of a function as a black box, return controls the output. It has no meaning outside of a function. Usually we use print() in this situation.

Would somebody PLEASE post a link to this exercise. Thank you.

NVM

https://www.codecademy.com/courses/learn-python-3/lessons/string-methods/exercises/review-ii

Okay, so I tried again and now it works simply with range(len(titles)) instead of range(0, len(titles)), so I am not sure what changed, but problem fixed… Thank you!

Hi there!
Thank you for your answer. I have been waiting for quite long and nobody answered.
I checked it again, and the last part of your solution is a bit different, than mine logically, but the output should be the same. And you also use range(0, len(titles)) instead of just range(len(titles)) - why is that I wonder…
I also checked back to this exercise and tried it simply with range(len(titles)) instead of range(0, len(titles)), and now it works fine… either I am schizophrenic or they just fixed a bug.
Anyway, thank you for your feedback! :slight_smile:

Core1320869428 via Codecademy Forums <[email protected]> ezt írta (időpont: 2020. jún. 4., Cs, 19:54):

Hello all,

This is my first post, so I apologize if this has been answered elsewhere, but I can’t seem to find it at the moment. I just finished the linked String Method Review.

My question relates to item 10. The following code was accepted by the exercise as sufficient and gave me the go ahead to move on. However, when I try to print, i get “<function poem_data at 0x7ff79450be18>” which is not the intended output. What is the specific explanation on this? Why was I able to proceed?

Question 10:

def poem_data(titles, poets, dates):
  poem_data = "The poem {title} was published by {poet} in {date}.".format(title=titles, poet=poets, date=dates)

Sounds like you are trying to print,

print(poem_data)

and seeing the correct result, under those circumstances.

Aside

It looks like one poem, one poet, and one date are being passed in. I’m a bit bewildered by the pluralized parameters.

def poem_data(titles, poets, dates):

Now, what can we expect from your function? It makes an assignment, and nothing else. The return value from that function will be None if we happen to invoke it.

print (poem_data('The Raven', 'Edgar Allan Poe', 1845))
# None

This suggests the aim of the function is to return a formatted string…

def poem_data(title, poet, date):
    return f"The poem, '{title}' was published by, {poet} in, {date}."

print (poem_data('The Raven', 'Edgar Allan Poe', 1845))
# The poem, 'The Raven' was published by, Edgar Allan Poe in, 1845.

@mtf,

Thank you for the insight and clarification. The pluralized parameters were a reference to the lists above that section of code where the information was stored. I did not include that in the question; apologies.

I appreciate your time, it really helps.

Thanks.

1 Like

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

Sorry, will you be able to explain the concept of this solution?

Lists and other linear sequences have a serial index starting from zero. range() generates a number sequence of integers, which above also begins at zero, and ends at the length of the list, excluding that value.

If a list has 10 elements, then,

range(0, len(the_list))

will be,

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

which corresponds to all the elements in the list. Now we can access each element by its index.

for i in range(0, len(the_list)):
    print (the_list[i])

In your example there are three equal length lists, titles, poets, dates, whose elements correspond at each index.

Not sure that answers your question. Is it the print statement you are asking about?

if you are having trouble with range() you can try this first to understand the program.

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

count = 0
for name1 in titles:
  lol ='The poem {} was published by {} in {}.'.format(titles[count], poets[count], dates[count])
  print(lol + '\n')
  count += 1

I passed the exercise, even though my code gave error of: return outside function.

I tried to make sure the code was within my loop and even checked if, somehow, my loop wasn’t idented.

Can someone give me a light on what might be going on?

return outside function

What can we tell you that you don’t already know? We’d need to see your code, and read the instructions to get a clear picture. Please post your code. Is this the problem linked to on 24 May, above?

I think you’re right. And by the return outside fuction, I think there is actually no solution, other than putting a print statement.

I believe what I was trying to do "
For [temp_var] in [list]:
return "

is actually impossible, and I believe that’s not the first time I commit a mistake with return.

Thanks mtf!

1 Like

Hi @rishidawar, can you walk me through why do you use (titles[-1],poets[-1],dates[-1])). I tried changing -1 to 0 or 1 and the code doesn’t work. But i cant figure out why -1 though.

Indexing a list with [-1] is a way of asking for the last element in that list. So in the given example if you performed a standard .append() to your list and addressed the same list with [-1] immediately afterwards you’d return the most recent element you added.

A quick example might help with negative indices if you’ve not used them before-

x = [0, 1, 2, 3, 4, 5]
print(x[-1])  # final element
->Out: 5
print(x[-1] is x[5])  # the same object?
->Out: True

See the following answer, at the end for how the index works with negatives-

2 Likes

Hi everyone, I think I wrote the right code, but I got a SyntaxError. I looked it up in Stackoverflow, but still don’t understand why. Can someone pls help me? Thanks.

Please post your raw code in a reply.

Sorry, here comes the code:

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

And the SyntaxError I received was for the line after the “print” code:

File “script.py”, line 30

                                                                                      ^

SyntaxError: unexpected EOF while parsing

Thanks for your help.