Try It! lambda squares between 30 and 70


#1



On this exercise:

https://www.codecademy.com/courses/python-beginner-en-KAgt5/3/3?curriculum_id=4f89dab3d788890003000096#


I was able to get it to display the right output by another forum post reply, however, the instructions suggested using list comprehension, and I tried but couldn't get mine working.

The commented line is my attempt to do this using list comprehension, but that resulted in a [0] output.


squares = [x**2 for x in range(1,11)]
#print filter(lambda x: (i for i in squares if i >=30 and i <=70), squares)
print filter(lambda x: (x >=30 and x <=70), squares)

Can anyone advise what I was doing incorrectly wth the list comprehension version?

Thank you


#2

squares is a derived list, which is all that the filter needs to run the lambda. There is no processing other than comparing elements in the list. If you wanted to combine the two,

print filter(lambda x: 30 < x < 70, [x**2 for x in range(1,11)])

The downside of this is we lose the ability to swap out the squares list.


#4

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.