# FAQ: Code Challenge: Loops - Reversed List

I appreciate your stance, but my personal learning path is simply mastery of the tools, and solving problems with those tools. I would counter and say if a loop isn’t necessary to accomplish the task, then it shouldn’t be used. An early mentor advocated not being a “hammer mechanic” which I’ve tended to stick to, meaning when you fix a car, you don’t just hit everything with a hammer, you use the appropriate tool to arrive at the appropriate outcome.

I do solve it with a loop, then I traditionally see if I can solve it a second or third time using less code. I learned to post with the spoiler recently, so I try to blur any solution I post.

2 Likes

Trying hard here not to respond with a metaphor addressing the best way to learn to become a master mechanic.

I’ll just point out that your “solution” does feature a nice example of returning a boolean comparison…

1 Like

Haha like I said, I do solve it with a loop first, I just try to iterate and optimize once or twice before I move on as well. Don’t worry, no step skipping over here

1 Like

Another issue is that lst2 is mutated, which may not be desirable if we wish to keep both lists intact and unchanged.

``````return lst1 == lst2[:].reverse()
``````

Above we mutate a shallow copy, not the original.

Would need to create the copy first. Then reverse it. Then compare. `.reverse()` returns ‘None’, so all comparisons evaluate to `False`.

1 Like

Good catch.

``````    temp = lst2[:]
temp.reverse()
return lst1 == temp
``````
1 Like

hello there please why in this exercice this code doesn’t work
def reversed_list(lst1, lst2):
for i in range(len(lst1)):
if lst1[i] == lst2[len(lst2)-1-i]:
return True
return False

The code does not confirm that one list is the reverse of the other one; it only returns a True on the first matched element. It is not a viable solution.

1 Like

I did it fairly differently as well. The site tells me it doesn’t work, but I’m getting the result I want, so I’m not sure what the problem is.

The first problem is that `i` is not an index, but a value.

The second problem is that return True is inside the loop. We don’t want to return on the first match, but rather the first non-match.

``````for i in range(len(lst1)):
if lst1[i] == lst2[-(i + 1)]: continue
else: return False
return True
``````
``````>>> reversed_list([1, 5, 3], [1, 5, 3])
False
>>> reversed_list([1, 5, 3], [3, 5, 1])
True
>>>
``````

Shouldn’t you be testing with the input data it says it fails on?

Hi actually my query is from the project Boredless Tourist. please find the following code, in that the (2nd last fn)find_attractions function works just fine when called separately but when i pass it onto the following (last fn)get_attraction_for_traveler it is returning a empty list. can someone clear me please.
Thanks

``````destinations = ["Paris, France",
"Shanghai, China",
"Los Angeles, USA",
"Sao Paulo, Brazil",
"Cairo, Egypt"]
test_traveler = ['Erin Wilkes', 'Shanghai, China', ['historical site', 'art']]

def get_destination_index(destination):
destination_index = destinations.index(destination)
return destination_index

print(get_destination_index("Paris, France"))

def get_traveler_location(traveler):
traveler_destination = traveler[1]
traveler_destination_index = get_destination_index(traveler_destination)
return traveler_destination_index

test_destination_index = get_traveler_location(test_traveler)
print(test_destination_index)

attractions = [[] for destination in destinations]

try:
destination_index = get_destination_index(destination)
attractions_for_destination = attractions[destination_index].append(attraction)
except SyntaxError:
return

add_attraction("Paris, France", ["the Louvre", ["art", "museum"]])
add_attraction("Paris, France", ["Arc de Triomphe", ["historical site", "monument"]])
add_attraction("Shanghai, China", ["Yu Garden", ["garden", "historcical site"]])
add_attraction("Shanghai, China", ["Yuz Museum", ["art", "museum"]])
add_attraction("Shanghai, China", ["Oriental Pearl Tower", ["skyscraper", "viewing deck"]])
add_attraction("Los Angeles, USA", ["LACMA", ["art", "museum"]])
add_attraction("Sao Paulo, Brazil", ["Sao Paulo Zoo", ["zoo"]])
add_attraction("Sao Paulo, Brazil", ["Pátio do Colégio", ["historical site"]])
add_attraction("Cairo, Egypt", ["Pyramids of Giza", ["monument", "museum"]])

def find_attractions(destination, interests):
destination_index = get_destination_index(destination)
attractions_in_city = attractions[destination_index]
attractions_with_interests = []
for attraction in attractions_in_city:
possible_attraction = attraction
attraction_tags = attraction[1]
for interest in interests:
if interest in attraction_tags:
attractions_with_interests.append(possible_attraction[0])

return attractions_with_interests

la_arts = find_attractions('Paris, France', ['monument'])
print(la_arts)

def get_attractions_for_traveler(traveler):
traveler_destination = traveler[1]
traveler_interests = [2]
traveler_attractions = find_attractions(traveler_destination,traveler_interests)
return traveler_attractions

smills_france = get_attractions_for_traveler(['Dereck Smill', 'Paris, France', ['monument']])

print(smills_france)
``````

Hello, @harishramesh6788.

Please re-post your code following the guidelines found here: How do I format code in my posts?

this line looks wrong:

``````traveler_interests = [2]
``````

you create a new list with a number, shouldn’t it retrieve something from the `traveler` list/function parameter?

Thanks mate, I fixed it and its working

Thanks for the guidance mate.! I appreciate it.

1 Like