Codecademy Forums

Did you double check the placement of `return`?

Question

As per this lesson, one common user challenge is the placement of return within their functions.

Answer

To quote python’s documentation: " return leaves the current function call with the expression list (or None ) as return value." The most important thing to take note of here is that it leaves the function. This means that when return is entered, the function will end. So return values are mutually exclusive. A function cannot hit return True over and over until its false, it will end as soon as it touches the first return. This quote below is a good metaphor:

In regards to the current lesson, here is a hint from this same moderator:

I hope these help, feel free to discuss below and help each other solve this challenge.

1 Like

This code isn’t making any sense to me. Here’s what I’ve got:

def reversed_list(lst1, lst2):
for i in range(len(lst1)):
if lst1[i] == lst2[(-i - 1)]:
return True
else:
return False

Here’s my problem:
when I have the second print() run through my code, it shows up as true.
The two lists are lst1 = [1, 5, 3] and lst2 = [3, 2, 1]

So in theory, on the second run-through of the loop, it’s doing this:
if lst1[1] == lst2[-1 - 1]:
return True
else:
return False

I know that lst1[1] is equal to 5 and lst2[-1 -1] is lst2[-2] which is equal to 2. These are clearly not equal, yet I’m getting a True return.

Can someone explain why this is?

1 Like

unfortunately, your code never gets to the second iteration/run through of the loop

By default, a function returns None at the end of the function. If we want to return something else at the end of the function, we can use the return keyword.

Given return is the last thing a function does, when a return keyword is reached, the function ends

6 Likes

I think I understand. So I only want to stop the function by returning if the return gives the answer I’m looking for. Otherwise the return needs to function outside of the loop?

The moment you determine list b isn’t the reverse of list a, you could return False. You don’t have too make the remaining iterations, given you already know what you need to know

2 Likes

You should return when you’re done. Doing it any earlier makes no sense at all and doing it any later is pointless.

3 Likes

image
here its alright if i do this, the new value of lst2_new comes out just fine. but then i put the full code
"def reversed_list(lst1,lst2):
lst2_new=
while len(lst1)>len(lst2_new):
lst2_new.append(lst2[-1])
lst2=lst2[0:-1]
if lst2_new == lst1:
return True
else:
return False "
it doesnt work.


see it doesnt work, why???

if anyone could post the official solution here I would appreciate it.
The following supposedly “solves it” but it still doesn’t solve for reversed_list(, )

def reversed_list(lst1, lst2):
for i in range(len(lst1)):
for i in range(len(lst2)):
if lst1[i] != lst2[i * -1 - 1]:
return False
else:
return True

#Uncomment the lines below when your function is done
print(reversed_list([1, 2, 3], [3, 2, 1]))
print(reversed_list([1, 5, 3], [3, 2, 1]))
print(reversed_list(, ))

We cannot see the indentation, but if the return statements are inside the loop, then the program does not run to completion.

Do we really need a nested loop? Can we do this with a single loop? You have the idea, already to use negative indices on one list.

s = [0, 1, 2, 3, 4, 5, 6]

Given the above list, let i equal 0 and let j equal -i - 1

i == 0; j == -1
s[i] == 0
s[j] == 6

Increment i and compute j.

i == 1; j == -2
s[i] == 1
s[j] == 5

Increment i and compute j.

i == 2; j == -3
s[i] == 2
s[j] == 4

and so on to `i == 5; j == -6.

What this means is we can iterate over one list and compare lst1[i] to lst2[j]. If there is no match, return False, else do nothing (so no else clause). Once the loop completes a full run, then and only then, return True.

2 Likes

Thank you very much.

1 Like

Can anyone please help me in understanding why my code is not working:

def reversed_list(lst1,lst2):
  lst2rev = lst2[-1::-1]
  for index in range(len(lst1)):
    if lst1[index] != lst2rev[index]:
      return False
    return True

it’s returning True for this list - print(reversed_list([1, 5, 3], [3, 2, 1])).

The problem is it is only reading the first index i.e. lst1[0] and comparing 0th index of the reversed list. Can you please help in explaining why it’s not iterating over rest of the indexes.

Thank you

return is the keyword used to pass back a return value to the caller of the function. After doing so, the function is done, kind of like handing in an exam

if the return keyword is nested in a loop, the return keyword will simply “stop” the loop. Like an exam where you haven’t completed all the questions but you decide to hand the test in (return it to the teacher)

2 Likes

Thank you for the explanation, however there have been cases of loops where return is used and the loop has iterated over all the elements without returning after the first one. So in this code which I posted earlier what should I modify - I used print in place of return and in the output I get comparison between each and every element of the two lists, printing True/False 5 times.
This is the code in the answer to the problem, and this one is working just fine despite using return -

def reversed_list(lst1, lst2):
  for index in range(len(lst1)):
    if lst1[index] != lst2[len(lst2) - 1 - index]:
      return False
  return True

except in this case, return True is outside the loop, where in your case it was nested in the loop.

which gives quite a difference, the correct solution only gives true when lst2 is lst1 in reverse

your function gives true if the first item of lst1 equals the last item of lst2.

6 Likes

Thank you so much! This explains.

image
Why is this wrong?
when flipping True and False and changing == for != it is correct… I don’t understand why can’t it be done the other way around.
HELP!

because return is the last thing a function does, when a return keyword is reached, the function ends

so with the code you currently have, if the first element of lst1 equals the last element of lst2, true will be returned, thus the function ends. At this point, we haven’t even verified all the other elements in the lists, so we can already have a false positive.

lets say the first element of lst1 doesn’t equal the last element of lst2 (at which point false should be returned, which isn’t happening)

the loop will continue with the second and element second to last element of lst2, compare them, if they equal, return true. Yet again false positive

5 Likes

Why does the code below not work?

def reversed_list(lst1, lst2):
  lst3 = lst2[::-1]
  for index in range(len(lst1)):
    if lst1[index] == lst3[index]:
      return True
    else:
      return False

It returns True for both tests!

return means handing back data, which means the function is done. However, in your program, this means the loop only makes a single iteration.

so the only compared are the first value of lst1 and the last value of lst2. If these values are equal, your program assumes lst2 is the reverse of lst1.

3 Likes