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 and Filter Comprehension

list comprehension

As a dumb American, I don't understand Celsius temperatures. Using c_to_f and a list comprehension, create a variable named good_temps. Convert each Celsius temperature into Fahrenheit, but only if the Celsius temperature is between 9 and 32.6.

temps.py
temperatures = [
    37,
    0,
    25,
    100,
    13.2,
    29.9,
    18.6,
    32.8
]


def c_to_f(temp):
    """Returns Celsius temperature as Fahrenheit"""
    return temp * (9/5) + 32

def is_9_32_6(temp):
  return  (temp >= 9) and (temp <= 32.6)

good_temps = [is_9_32_6(temp) for temp in map(c_to_f, temperatures)]

1 Answer

Steven Parker
Steven Parker
220,415 Points

Close, but you're converting the temperatures first with map, and then using the list comprehension to return a true or false for each based on if the converted temperature is in the range.

But you want to return a converted temperature for each Celsius one that falls in the range.

Hint: you won't need a map.

And if you still can't get it...


:warning: SPOILER ALERT


good_temps = [c_to_f(temp) for temp in temperatures if is_9_32_6(temp)]

Great It works

Thank you