My code is not working def is_even(): n % 2 == 0 return n = int(input("Enter the Value: ")) nums = [] for i in range(1,n+1): nums.append(i) print(nums) even_num = filter(is_even,nums) print(list(even_num))
def is_even(): n % 2 == 0 return The function is_even does not take any parameters. It should take a number as an argument. The expression n % 2 == 0 should be in the return statement. It should return a boolean value (True if the number is even, False otherwise). This should work. def is_even(n): return n % 2 == 0 n = int(input("Enter the Value: ")) nums = [] for i in range(1, n + 1): nums.append(i) print(nums) even_num = filter(is_even, nums) print(list(even_num))
x = [1,2,3,4,5,6]
for i in x:
if i%2 != 0:
x.remove(i)
print(x)
😎
I wonder if there's any example of filter than can't be done with list comprehension
There is, so long as it follows certain logic
It should be a lot faster and a lot more memory efficient
My code is not working
def is_even():
n % 2 == 0
return
n = int(input("Enter the Value: "))
nums = []
for i in range(1,n+1):
nums.append(i)
print(nums)
even_num = filter(is_even,nums)
print(list(even_num))
def is_even():
n % 2 == 0
return
The function is_even does not take any parameters. It should take a number as an argument.
The expression n % 2 == 0 should be in the return statement.
It should return a boolean value (True if the number is even, False otherwise).
This should work.
def is_even(n):
return n % 2 == 0
n = int(input("Enter the Value: "))
nums = []
for i in range(1, n + 1):
nums.append(i)
print(nums)
even_num = filter(is_even, nums)
print(list(even_num))