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

where am l getting it wrong..

Create a function named reverse that takes a single iterable item and returns a reversed version of that item

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

def reverse(iters):
    iters = ['this']
    modified = list(map(reverseStr,iters))
    return modified

2 Answers

Steven Parker
Steven Parker
231,268 Points

Did you peek ahead at code for task 2? You won't need "map" for task 1.

Here's a few hints:

  • the "reverse" method should work with the argument, it should not replace it with literal data
  • it should work with any kind of iterable (strings, lists, tuples, etc)
  • a handy mechanism that works with all iterable types is a slice
Steven Parker
Steven Parker
231,268 Points

Rutendo Chaurura — You can mark a question solved by choosing a "best answer".
And happy coding!

James Arnold
James Arnold
3,986 Points

Create a function named reverse that takes a single iterable item and returns a reversed version of that item.

Here we'll create our function - reverse - that takes in our list - backwards - and returns our reversed version. We'll do this by iterating through the list with a -1 step to reverse the order.

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

def reverse(iters):
    return iters[::-1]

Now use map() to create a variable named forwards. forwards should use reverse() to reverse the order of every item in backwards.

Now that we've got our function done, we'll come back to the global scope to create our forwards variable. Here we'll use map() to place in our function - reverse - and the object we want to iterate through - backwards.

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

def reverse(iters):
    return iters[::-1]

forwards = map(reverse, backwards)
Steven Parker
Steven Parker
231,268 Points

This is a great illustration of your own solution, but sometimes folks can get a better learning experience when you provide some hints that help them resolve the issue for themselves. :wink:

James Arnold
James Arnold
3,986 Points

Steven Parker - I don't disagree! I try to help based off of how close the original poster is. Hopefully, reading through the question breakdown and seeing what code was added will give them their 'Ah ha!' moment. Cheers!

thank you

Ryan McGuire
Ryan McGuire
3,758 Points

For sure Steven Parker, but sometimes the answer is needed for that eureka moment when the student is at too much of a loss. I still do not understand why TreeHouse structures at least the Python related tracks the way it does. Seems as though there are large gaps or jumps. Granite, for this price I don't expect it to be perfect, but still seems odd.