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 Comprehension

Andrew Winkler
Andrew Winkler
37,739 Points

What am I doing wrong here? reduce vs list comprehension?

I've re-watched the video, looked through the docs, and am now resorting to trial-and-error. Can someone please go ahead and explain this to me? I still don't fully understand what a list comprehension entails as well.

maps.py
dimensions = [
    (5, 5),
    (10, 10),
    (2.2, 2.3),
    (100, 100),
    (8, 70),
]

def area(tup):
   return tup[0] * tup[1] 

areas = reduce(add, map(area, dimensions))

1 Answer

Steven Parker
Steven Parker
231,268 Points

First some hints: You won't need reduce. And a list comprehension is a list, so it will be enclosed in []'s, and will have a kind of a modified loop syntax inside. In fact, you could get the same result using a loop. And remember to use your area function.

Got it now? If not, you might try reviewing the video.

If that still doesn't help, and you'd like to peek at an answer...


:warning: Spoiler Alert


areas = [ area(item) for item in dimensions ]
Andrew Winkler
Andrew Winkler
37,739 Points

Thank you. This was so easy, I just didn't see it. I was anticipating something more advanced.