Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

Am I reversing 'backwards' correctly?

Here's my code. I was sure this was correct, and after checking the threads, it seems to be the popular answer. However, I am not passing the code challenge. Any insight? I'm getting Bummer! Not all items were reversed correctly. I'm inquiring about challenge 2 of 2 (forwards).

backwards = [ 'tac', 'esuoheerT', 'htenneK', [5, 4, 3, 2, 1], ]

def reverse(thing): return list(reversed(thing))

forwards = map(reverse, backwards)

It looks like your method reverse() takes a parameter, but I can't figure out what "thing" is for your argument.

3 Answers

Hola,

Reverse is a function that you call against a data structure, with the exception of a string. Since strings don't have reverse. Check out these examples and let me know if it all makes sense.

list1 = ['hello', 'world']
list1
['hello', 'world']
list1.reverse()
list1
['world', 'hello']

What happens here is that I have a list called list1. You must be careful here because reverse actually reverses the value in place. It doesn't make a copy of it. It actually reverses the value stored in memory.

Now, if I try it with a string as I mentioned above.

>>> str = "hello"
>>> str.reverse()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'str' object has no attribute 'reverse'
>>>

You can see string has no built in function called reverse like a list does. How did I know that you might ask? I used dir() another built in function, to see if the reverse() function is available.

dir(str)
# returns a lot but if you look at the output there is no reverse()

dir(list1)
#returns a lot but you will notice a built in function reverse() listed. 
'__reversed__'

Now why is this important? Well, in this example you have a list inside of a list that also contains strings. Let me explain more.

# This is your text note you do not need the comma at the end.  I fixed it below.
backwards = [ 'tac', 'esuoheerT', 'htenneK', [5, 4, 3, 2, 1], ]   

#fixed
backwards = [ 'tac', 'esuoheerT', 'htenneK', [5, 4, 3, 2, 1]]

# The backwards list, actually has a list of strings for it's values AND one of the values is also another list. 

Let's put it all together now. If I want to reverse the entire list. I can. I show you how below. If I want to reverse just a value inside the list. I can do that too BUT BUT BUT, I can not reverse strings, since they do not have a built in method for reverse(). But I can reverse one of the values, since one of them is another list. And I can reverse lists as shown above. So to demonstrate it all.

# I have your list here.
>>> backwards = [ 'tac', 'esuoheerT', 'htenneK', [5, 4, 3, 2, 1]]

#Print your list and you can see it's like I made it. Nothing has changed.
>>> backwards
['tac', 'esuoheerT', 'htenneK', [5, 4, 3, 2, 1]]

#reverse the list since I can reverse lists, they do have the built in reverse function
>>> backwards.reverse()
>>> backwards
[[5, 4, 3, 2, 1], 'htenneK', 'esuoheerT', 'tac']

#Now I want to reverse part of my list. BUT wait, I can't since the
#part I am trying to reverse is actually a string. Which if you remember
#does not work. So I get the no attribute reverse, which is the same 
#as saying string does not have the reverse built in function
>>> backwards[3].reverse()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'str' object has no attribute 'reverse'

#Now I do have a list inside a list. So can i reverse that? Answer is yes.
# I use [] to indicate a value inside a list. In this case since I reversed the list
# The string is no longer at the end but the beginning. So I use [0]
>>> backwards[0].reverse()
>>> backwards
[[1, 2, 3, 4, 5], 'htenneK', 'esuoheerT', 'tac']

#That worked it reversed the value above. Now, let's put it back the way it was
# by reversing it again, except this time. Notice how the list inside a list, the list of 
# integers (numbers). Is still reversed from when I started. 
>>> backwards.reverse()
>>> backwards
['tac', 'esuoheerT', 'htenneK', [1, 2, 3, 4, 5]]
>>>

#I could of accomplished this same thing by just reversing the original list
# and using the following

backwards[3].reverse()

#but then I wouldn't get to explain all this to you.

Built in methods are called on an object to perform some sort of task. Reverse is not actually a function. Now this may be confusing since yes if I create my own class and I want it to be reversed I can write a reverse() funciton inside my class to call. So that any data inside my class can be reversed. But that is far beyond the scope of this exercise.

SUMMARY Reverse is a method I call against some set of data to reverse it's order. I can use the dir() method to determine if reverse() can be called against an object. Reverse also reverses the data in place. Doesn't make a copy. if I do

>>>y = backwards.reverse()
y
>>>

Y is not my new value and backwards is still the same. Backwards actually gets reversed still. Even if you assign it to y, which means y and backwards are identical and thus I get no response when trying to print y. Again, don't think about it to much. Just know it changes the data when called against data period. Think of it like that.

Now, why reverse() with brackets vs reverse without brackets. not all methods with () takes an argument in the form of which you are thinking.

I can use reverse or reverse(). The difference is reverse gives me back the object, while reverse() actually executes against the object. This comes into play when passing functions around. We wont go there since again, it;s out of the scope. But for reverse. We always call it with () for all intensive purposes.

Do you understand it now? If not let me know and I can try another way of explaining it.

Hey,

This ticket isn't linked but I can answer the question without the quize.

You have the right through process, you do need to create your own function. He ays to use reverse(), well that to me sounds like a method I could create to do the operation.

THIS IS WHAT YOU ATTEMPTED

def reverse(x):
    return x.reverse()

# Then do the map using reverse
forwards = map(reverse, backwards)

OH BUT WAIT, that doesn't work either. Since you can't reverse a string. We know that from my description above.

We could try using a lambda, right? Nope, same thing will happen.

So now what?

Let me ask one more questions.

  1. I need to replace each item, but I have integers and strings, a list inside a list. So I need something that is data type agnostic and doesn't care to reverse these values? Any ideas?

ANSWER

>>> backwards = [ 'tac', 'esuoheerT', 'htenneK', [5, 4, 3, 2, 1], ]
>>> def reverse(x):
...         return x[::-1]
...
>>> forwards = map(reverse, backwards)
>>> forwards
['cat', 'Treehouse', 'Kenneth', [1, 2, 3, 4, 5]]

Now I understand why you needed the extra ],] in backwards. I said remove that but I was't aware of the full context in which you were trying to solve the problem in. In either case, does this answer your question?

I really appreciate this Ryan Ruscett! You've been a great help.

Wow, Ryan, I greatly appreciate you taking the time to respond to my question in great detail! I do understand what you're showing me.

Here's the instruction to the challenge: Now use map() to create a variable named forwards. forwards should use reverse() to reverse the order of every item in backwards.

I understand what you were showing, but am not understanding how to write this (challenge question).