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

Map task, Functional Python

What could be wrong with this code? I feel like I'm asking all the petty questions haha but I'm studying all by myself so it's kind of hard.

from copy import copy

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

def reverse(iterable):
    iterable = copy(iterable)
    iterable.reverse()
    return  iterable

forwards = list(map(reverse, backwards))

1 Answer

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,423 Points

The issue is a str object does not have a reverse() method. When the map runs the following error is thrown:

/home/chrisf/Devel/Treehouse/questions/ethan-chae/map-2.py in <module>()
     13     iterable.reverse()
     14     return iterable
---> 15 
     16 forwards = list(map(reverse, backwards))

/home/chrisf/Devel/Treehouse/questions/ethan-chae/map-2.py in reverse(iterable)
     10 
     11 def reverse(iterable):
---> 12     iterable = copy(iterable)
     13     iterable.reverse()
     14     return iterable

AttributeError: 'str' object has no attribute 'reverse'

Note: The line numbers above are shown one higher than your code due to an extra line added above the function def

You will need to rewrite your reverse function to work for both strings and lists. HINT: look at using a slice to reverse the iterable: iterable[::-1]

Also note Task 2 is looking for a map object, not the list returned by applying the map. Your Task 2 answer can be changed to:

forwards = map(reverse, backwards)

Thank you Chris! Yeah, I was first thinking about slicing. Then the code passed using just reverse() function, so I just carried on. It worked :)