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

Neil Gordon
Neil Gordon
8,823 Points

quick question on using maps on a list in python

def area(nnn):
    return nnn*nnn
    print(list(map(nnn, dimensions))
pass
maps.py
dimensions = [
    (5, 5),
    (10, 10),
    (2.2, 2.3),
    (100, 100),
    (8, 70),
]


def area(nnn):
    return nnn*nnn
    print(list(map(nnn, dimensions))
pass
Neil Gordon
Neil Gordon
8,823 Points

Sorry forgot my question . i'm getting try again. I want to know if I am close or completely off. I'm trying to multiply the touple by itself

here is the original question: Create a function named area() that takes a single argument which will be a two-member tuple. area() should return the result of multiplying the first item in the tuple by the second item in the tuple.

2 Answers

Here is the answer. For the first part you just need to multiply the 0 index of the typle by the 1 index value. For the second part, it wants you to create a variable named areas that stores the list comprehension. You would to use the function as the first argument of maps and then the dimensions variable as the second argument. This will use each of the dimensions as the arguments for the area function.

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

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

areas = list(map(area, dimensions))
Neil Gordon
Neil Gordon
8,823 Points

Thank you John Lindsey that did the trick.

this doesn't work why

dimensions = [ (5, 5), (10, 10), (2.2, 2.3), (100, 100), (8, 70), ] def area(dimensions): products=[]

for x in dimensions:
    product = 1
    for element in x:
        product *= element

    products.append(product)
return tuple(products)

print(area(dimensions))