14/18 Try it!


#1

Why isn't this working?

list1 = range(1,11)
squares = [list1 ** 2]
print squares
print filter(lambda x: x >= 30 and x<=70, squares)


#3

This is my code i used to pass this lesson:

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

Hope that helps :slightly_smiling:


#4

To answer your question, it does not work because you are trying to square a list. A list isn't a number you can square. The list contains numbers which you could square but you didn't ask Python to square a number.

To do it your way you have to define the variable 'squares' with each number in 'list1' squared

i.e.

list1 = range(1,11)
squares = [list1[0] ** 2, list1[1] ** 2, list1[2] ** 2, list1[3] ** 2, list1[4] ** 2, list1[5] ** 2, list1[6] ** 2, list1[7] ** 2, list1[8] ** 2, list1[9] ** 2]
print filter(lambda x: x >= 30 and x<=70, squares)

A shorter version squares the numbers in the range right off the bat, and we can even shorten kqlyvacban's code by eliminating the 'and' like so:

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

#5

When you are doing squares=[list1**2] you are telling it to square the numbers 0-10. When it wants the numbers 30-70 to be filtered through. When you do
squares = [x**2 for x in range(1,11)]
it applies it through
print filter(lambda x: x >=30 and x <=70, squares)
So squares tells it to square those numbers 30-70.


#6

here is my code in which i have used comprehension method first..
you can check this
squares = [x ** 2 for x in range(1,11)]
print filter(lambda x: x >= 30 and x <= 70, squares)


#7

I have:

squares = [x ** 2 for x in range(1, 11)]

print filter(lambda x: x >= 30 and <= 70, squares)

I get:

File "python", line 3
print filter(lambda x: x >= 30 and <= 70, squares)
^
SyntaxError: invalid syntax

I don't understand. The arrow points at <=


#8

Needs to look like this: x >= 30 and x <= 70


#9

wow thank you! this was so...so....small


#10

You should try make your list "squares" a list comprehension.

Here's the conclusion I came to:

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

Good luck! :smiley:


#11

Instead of x >= 30 and x<=70, you can also write:
squares = [x**2 for x in range(1,11)]
print filter(lambda x: x in range(30, 71),squares)