Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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
218,812 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.