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 trialAndy Hughes
8,479 PointsTrying 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. :)
backwards = [
'tac',
'esuoheerT',
'htenneK',
[5, 4, 3, 2, 1],
]
def reverse(item):
return sorted(backwards, key=itemgetter([]), reverse)
1 Answer
Steven Parker
231,268 PointsAdvanced 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.
Chris Freeman
Treehouse Moderator 68,441 PointsChris Freeman
Treehouse Moderator 68,441 PointsFixed slices link.
Andy Hughes
8,479 PointsAndy Hughes
8,479 PointsThank 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
231,268 PointsSteven Parker
231,268 PointsYou are so right about time and practice! 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!