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

Andy Hughes
Andy Hughes
8,114 Points

Map comprehension. Question is confusing!

So once again, I've wasted more time than needed, overthinking the question for this challenge.

If you're doing part one, just ignore the 'dimensions' list because it's largely irrelevant for part 1. At first when I read the question and it said 'create a function named area that takes two arguments from a tuple list, I ended with this:

def area([arg1, arg2]):

(Ok so I'm still a noob!) The question doesn't mean that you're writing two arguments into the function. It simply means that the list contains a tuple with a series of paired arguments. So you need to be aware that you will reference a first item and second item.

In our area() function, our argument is going to be the variable 'dimension', which is a single argument. In essence, ignoring the 'dimensions' list altogether, this is like saying, create a function that will multiply two items together that are contained in one variable.

However, because the 'dimensions' variable contains paired items in the list, our function needs to know how to choose the first item and second items independently. So remembering [keys], we can reference our argument 'dime' and just tell it whether it's the first item '[0]', or the second item '[1]'.

For the second part, we're simply writing a list comprehension style query as per the end of the 'Map' video. Kenneth shows it a couple of minutes before the end.

Hope that helps someone else as it confused the heck out of me at first!

dimensions = [
    (5, 5),
    (10, 10),
    (2.2, 2.3),
    (100, 100),
    (8, 70),

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

areas = [area(dime) for dime in dimensions]

1 Answer

Mel Rumsey
.a{fill-rule:evenodd;}techdegree seal-36
Mel Rumsey
Treehouse Staff

Andy Hughes Thank you for sharing your thought process and how you worked through this. Nice job and I am sure it will help someone else if they get stuck on this one! ^-^