FAQ: Code Challenge: Loops - Reversed List

This community-built FAQ covers the “Reversed List” exercise from the lesson “Code Challenge: Loops”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Join the Discussion. Help a fellow learner on their journey.

Agree with a comment or answer? Like () to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

A post was split to a new topic: Code challenge bug [solved]

5 posts were split to a new topic: Return will end a function and stop any loops

25 posts were merged into an existing topic: Return will end a function and stop any loops

6 posts were merged into an existing topic: How can I iterate over a list backward?

4 posts were split to a new topic: Could I just use `reverse()`?

I am able to solve most of these but I am often going about it much differently than I see in the comments.

def reversed_list(lst1, lst2):
–i = v = 0
–if len(lst1) == len(lst2):
----for num in lst1:
------while i < len(lst1):
--------rev = lst2.pop()
--------if lst1[i] == rev:
----------i += 1
--------else:
----------i += 1
----------v += 1
–else:
----v += 1
if v != 0:
–return False
else:
–return True

Should there really be two loops there? Seems to me like you’d iterate over one of the lists, and then you’re done, there’s nothing to repeat.

There are also two places you could exit early: failing the length check (avoids nesting the rest of the code inside an if-statement), and upon finding a difference (avoids having to count, either it exits early, or they are the same)

One of the buttons in the post editor is for telling the forum to not re-format things.

1 Like

I honestly don’t have a clue why this line works, specifically the - index part. I understand the rest of the code just fine when I read it out loud…

``````if lst1[index] != lst2[len(lst2) - 1 - index]:
``````

What does - index do exactly? All I know so far is that we take any index from lst1 and if not equal to the last index of lst2 - index(???).

lets say we have the following two lists:

``````lst1 = ['a', 'b', 'c', 'd', 'e']
lst2 = ['e', 'd', 'c', 'b', 'a']
``````

lst1 is the same as lst2 in reverse. Now we have to express this into code

what we need to do:

• compare first item of lst1 with last item of lst2
• compare second item of lst1 with second to last item of lst2
• compare last item item of lst1 with first item of lst2

(`...` represents everything in between, i was too lazy to write them all)

this exactly what this if condition does, for the first iteration of the loop, `index` will be zero, so then we get:

``````if lst1[0] != lst2[len(lst2) - 1 - 0]:
``````

lets do the math first, lst2 has a length of 5 (sticking with the example i started this post with). 5 - 1 - 0 is 4, so we get:

``````if lst1[0] != lst2[4]:
``````

now we resolve the square brackets, which gives us the values from the lists based on indexes:

``````if 'a' != 'a':
``````

these are equal, so the lst1 could still be the same as lst2 in reverse, so we continue to the next iteration of the loop, index has now become `1`:

``````if lst1[1] != lst2[len(lst2) - 1 - 1]:
# resolving math
if lst1[1] != lst2[3]:
# getting values/resolving square brackets
if 'b' != 'b':
``````

these are equal, so the lst1 could still be the same as lst2 in reverse. Here we compared the second value of lst1 with the second to last value of lst2.

This continues until we completed all the elements from the list, or one of the values doesn’t equal, in which case lst1 isn’t the same as lst2 in reverse, in which case we should return False

5 Likes

Ohh now I get it, thank you for your through explanation! Some of these concepts really need some time to sink in.

I found this to be the easiest way.
Decided to check if anyone else had the same idea but I didn’t see it here so I figured I’d share.

``````def reversed_list(lst1, lst2):
if lst1 == lst2[::-1]:
return True
else:
return False
``````

given a comparison gives a boolean value as result, we can just return that:

``````def reversed_list(lst1, lst2):
return lst1 == lst2[::-1]
``````
1 Like

I created an empty list in which i could store every True value. This would add a True value to that list (line 7) every time there was a match. In line 8 I compared the length of the list with the counted amount of True’s. This would then return True if every comparison was a match and False otherwise.
Is there any weak point in this code? I am wondering because it differs from the original solution

Counting the amount of matches will cause it to ignore where the matches are, won’t it? You’d for example get the result that `[1, 2]` is the reverse of `[1, 2]` which it isn’t, due to having the same elements.
`[1, 1]` is also the reverse of `[1, 1]` but your function probably says that it’s not due to the number of matches being 4
Also seems like far too many comparisons. How many comparisons should be made with regard to the size of the input?

Whether it matches the “solution” is kind of irrelevant. Is it doing the right thing? Would you be doing those things if you did it manually? What is the right thing to do? Ask yourself what should happen and refer to that when you write the code.

2 Likes

Thanks for the comments. I thought this code would result in the same output that was give as the original solution. I was struggling with the exercise which resulted in me trying another approach. I think I tried too hard to make the example lists fit the code that I lost sight of the bigger picture. Thanks anyways

A possible solution:

``````#Write your function here
def reversed_list(lst1, lst2):
lst3 = []
for n in lst2:
lst3.insert(0, n)
print(lst1, lst2, lst3)
if lst1 == lst3:
return True
else:
return False

#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([2, 7, 3, 1], [1, 3, 7, 2]))
print(reversed_list([2, 7, 3, 1], [1, 7, 3, 2]))
``````

And this generates and checks automatically the pair of lists:

``````##########
#This imports the random module for the random list generator.
import random

##########
# Random list generator.
# Generates a list with 5 integer numbers, among 0 and 9.
# The variable "ctrl" controls the while loop condition.
# You can generate larger lists changing the while loop condition.
def random_list_generator():
ctrl = 0
lst = []
while ctrl != 5:
ctrl += 1
lst.append(random.randint(0, 9))
return lst

##########
# Checks if "lst2" is the reversed version of "lst1".
# Generates a specular list of "lst2" called "lst3", and cheks if "lst1" (...)
# (...) and "lst" are equal.
# Returns "True" or "False".
def reversed_list(lst1, lst2):
lst3 = []
for n in lst2:
lst3.insert(0, n)
if lst1 == lst3:
return True
else:
return False

##########
# Variable "x" controls the main while loop.
# Variable "iterations" permits both while loop control and information (...)
# (...) about how many iterations need the while loop to find a positive (...)
# (...) result (= "True").
# The results will be printed if there is a positive match.
# The iterations are caped at 1.000.000 for avoid infinite loops.
x = 1
iterations = 0

while x == 1 and (iterations <= 10**6):
random_list1 = random_list_generator()
random_list2 = random_list_generator()
if reversed_list(random_list1, random_list2) == False:
iterations += 1
else:
iterations += 1
x = 0
print(reversed_list(random_list1, random_list2),
random_list1, random_list2, iterations)
``````

This is what I did…

the other way i.e. “the hint”, while I can see what it does…sort of…confuses me.

And now for a spot of silliness…

``````>>> a = [3, 5, 7]
>>> b = [8, 5, 3]
>>> c = zip(a, b[::-1])
>>> d = map(lambda x: set(x), c)
>>> e = filter(lambda x: len(x) != 1, list(d))
>>> list(e)
[{8, 7}]
>>> a = [3, 5, 7]
>>> b = [7, 5, 3]
>>> c = zip(a, b[::-1])
>>> d = map(lambda x: set(x), c)
>>> e = filter(lambda x: len(x) != 1, list(d))
>>> list(e)
[]
>>>
``````
1 Like
``````def reversed_list(lst1, lst2):
a = 1
for i in range(1, len(lst1)+1):
if lst1[i-1] != lst2[-i]:
a *= 0
break
return bool(a)
``````

I’ve defined the range to look more familiar - like 1-1 =[0] vs [-1], 2-1 =[1] vs [-2] etc. The function returns variable a which is equal to 1. In case the values in a pair don’t match each other, variable a turns into 0. I’ve put break here because the only inequalty in pairs is enough to conclude that lists don’t match the task, so we don’t need to run the loop further on. The function returns boolean value of variable a.