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 Functional Python Functional Workhorses Map

Andy Hughes
Andy Hughes
8,479 Points

Trying to reverse the list, but I'm stuck!

I have been trying to do this challenge for a while and totally stuck.

I've looked at community answers and many seem to use

[::-1]

inside their function. Where does that come from in relation to the videos on 'Sorting' and 'Map'? I can't seem to find anything like this when watching videos back.

My code below looks like the sort of thing Kenneth covered in the videos, but clearly it's incorrect.

Once again, some guidance would be appreciated please. :)

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


def reverse(item):
    return sorted(backwards, key=itemgetter([]), reverse)

1 Answer

Steven Parker
Steven Parker
231,275 Points

Advanced courses like this one assume you are familiar with the concepts presented in the beginner series. Slices aren't discussed in this course, but they were in the Slices video (the part about reversing starts at time 2:58) from the beginner-level course Python Sequences. And task 1 is just preparation for task 2 where you will use map.

There are a number of syntax and logical issues with the code here:

  • the parameter item isn't being used in the function
  • nothing has been defined for the identifier "backwards" to refer to
  • the itemgetter helper needs specific index value(s) as argument(s)
  • the "key" is not needed since the function only needs to work with simple iterables
  • the syntax for reversing in sorted is "reverse=True"
  • sorting isn't a viable approach to the task

An example of why sorting won't help is that "tac" sorted would be "act", or "tca" if sorted in reverse. But what the challenge is expecting to see when reversing "tac" is "cat".

Hint: a slice is well-suited to this task, or it could be done (with more steps) in a loop.

Andy Hughes
Andy Hughes
8,479 Points

Thank you Steven Parker. I totally get the assumption that I should be familiar with the basics, having done the course. In reality, the lack of daily repetitive use of the basics means it is easily forgotten. Hopefully in time, through repeated use, it will stay in my head. :).

Thanks also for a comprehensive answer. I've moved through the course quite quickly and so I'm still just too new to quite understand some of the things you're mentioning. But hopefully that's just time and practice.

I guess it's like going out for a driving lesson and then being expected to remember it all in the next lesson. It takes practice and repetition. :)

Steven Parker
Steven Parker
231,275 Points

You are so right about time and practice! :+1: And learning where to find info about things is at least as important as remembering thing things themselves (I certainly don't remember it all).

Happy holidays!   :christmas_tree: