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

this does not pass: def area(position): return position[0] * position[1] areas = map(area, dimensions)

I don't understand why this does not pass.

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

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

areas = map(area, dimensions)

2 Answers

Jesse Pavlis
Jesse Pavlis
18,088 Points

Hey Grégoire Mesnil,

The requirement for the second Challenge is to use List Comprehension. You are instead using the built-in function map(). I applaud your usage of python's built-in functions, and this is a good implementation of it, but the task requires the use of list comprehension.

areas = [position[0] * position[1] for position in dimensions]

ok thanks Jesse ! It's weird that it is in the map() class section though.

Trike Whipcat
Trike Whipcat
12,726 Points

Wow, I wish this example explicitly stated to use a list comprehension from the outset on task 1. The posted solution raises a type error on my local machine.

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

area(dimensions) TypeError: can't multiply sequence by non-int of type 'tuple'

This is attempting to multiply two tuples together: (5, 5) x (10, 10)

Wouldn't this have to be (dimensions[0][0])*(dimensions[0][1])?

I solved it using a function and got the expected results, but I know I'm missing the point of the exercise by not using map or list comprehensions.

def area2(arg):
    result=[]
    for x,y in arg:
        result.append(x*y)
    return result

def area(dimensions):
    return dimensions[0] * dimensions[1]
areas = [area(x) for x in dimensions]
print(area2(dimensions)==areas)