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

Robert Armstrong
Robert Armstrong
4,941 Points

Functional Python: Reverse function...

I've run the code included and I get a perfectly reversed version of the original iterable. Additionally, I've tried running this where the function only reverses the items in the iterable, not the order of the items, and it still fails. What am I doing wrong here?

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

def reverse(my_iter):
    my_new_iter = copy(my_iter)
    for n in range(0, len(my_iter)):
        reverse_item = my_new_iter[n]
        my_new_iter[n] = reverse_item[::-1]
    return my_new_iter[::-1]

print(backwards)
print(reverse(backwards))

1 Answer

Steven Parker
Steven Parker
231,275 Points

You're probably just getting too fancy. Keep it simple:

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

You were also using copy without first importing it.

Laurens Sandt
Laurens Sandt
3,513 Points

Thanks for your answer it is exactly what In was looking for