I don’t understand why my code is not working for the second set of lists, where i = 1.
your code only compares the first value of lst1 with the last value of lst2, if these values are equal, true is returned else false is returned.
After reading through the whole thread, I too, finally understand why Code 1 works to generate the correct and accurate solution instead of Code 2. This is an illustration of how it works. Please do correct me if there’s any sort of error.
e.g. - (reversed_list([1, 5, 3], [3, 2, 1]))
lst1 = [1, 5, 3]
lst2 = [3, 2, 1]
#Code 1 - Combing through list similarities with return False def reversed_list(lst1, lst2): for index in range(len(lst1)): if lst1[index] != lst2[len(lst2) - 1 - index]: return False return True print(reversed_list([1, 5, 3], [3, 2, 1])) #prints False *correct answer*
#Code 2 - Combing through list similarities with return True def reversed_list(lst1, lst2): for index in range(len(lst1)): if lst1[index] != lst2[len(lst2) - 1 - index]: return True return False print(reversed_list([1, 5, 3], [3, 2, 1])) #prints True *inaccurate answer*
Like what stetim94 mentioned:
If you use Code 2, which first uses return True to comb through similarities, it will give you inaccurate answers. Because it iterates over the value of the indices, it will return True upon the first match and stop iterating over the remaining values, which may not match 1-for-1 with the corresponding list.
Whereas if you use Code 1, which first uses return False to comb through similarities, it will stop iteration upon the first non-match of indices’ value. Else, the code will continue to run till the last index and return True for an all-matching pair of lists.
There is no point in using slicing AND a loop. Use just a loop, or just a slice.
n, m = len(lst1), len(lst2) if n != m: return False m -= 1 for i in range(n): if lst1[i] != lst2[m - i]: return False return True
Using a slice doesn’t give us much loop practice…
return lst1 == lst2[::-1]
Thanks for adding a line to as a first line of defence to check if the list has the same lengths.
Point taken for this:
In the first example, if just one character of the “reversed” list is in the correct position, the function will return True, even if all of the rest are incorrect. In the false example, 1 in lst1 compares equal to 1 in lst2, and the function is done, returning True.
Remember that once return is reached one time, the function returns its value and halts.
In the second example, a single incorrect value returns False, which (if you think about it) is what you want.
Could someone explain to me why to following code is incorrect?
It’s supposed to return “True” and then “False”, but I get “True” and “True” with this code. I know it has to do with the ‘else:’ part and probably something with indentation, but I don’t understand what the problem is…
It will return the condition (match or non-match) of the first index tested, and then halt, since what return does two things:
- Return the value to the calling statement, and
- Halt processing of the function
In other words, once return is reached one time, the function is over.
Can anyone explain to me what is the difference between the two?
Why one is correct and the other is not?
return happens at the end of function, handing data back to the caller
which means, in the second case, the moment any value (lets say the first value) equals the opposite value (last value in the list), true is returned, the function stops. Which isn’t the desired behavior.
you only want to return true after comparing all values, not at the first match.
You can return False when values do not match, and not compare the rest of the values.
What do you think about this solution?
It works. How about doing it on 3 lines or even a single line? Do you really need to
return True or
return False? What is returned by the following?
return 3 == 4 or
return 3 == 3
EDIT: Your solution has a flaw. What about these lists:
print(reversed_list([1, 2, 3], [1, 2, 3]))
print(reversed_list([1, 4, 5, 3], [3, 4, 5, 1]))
Your method will return
True for both when it should return
I made it with list comprehensions and it works)
def reversed_list(lst1, lst2):
new_list = [number for number in range(len(lst1)) if lst1[number] == lst2[len(lst2)-number-1]]
if len(new_list) == len(lst1):
Hello, @4210506100. Welcome to the forum. I love using list comprehension myself. You might consider whether you need an
if ... else to return
false? Using the list comprehension, your function could be reduced to a single line of code. One other point. Have you considered lists other than those provided in the exercise? How about:
print(reversed_list(, [1, 2]))?
Thank you! Yes i am new in forum and didn’t find how to change name))
Yes it won’t work with that input “print(reversed_list(, [1, 2]))”, but in this task it says that “two lists of the same size” - it’s work only if so
You are correct regarding the instructions. I just like to push the boundaries
just change the test expression and it will work :
def reversed_list(lst1, lst2): for i in range(len(lst1)): if lst1[i] != lst2[-1 - i]: return False return True print(reversed_list([1, 2, 3], [3, 2, 1])) #True print(reversed_list([1, 5, 3], [3, 2, 1])) #False