Program to filter even values from list using regular function
# Python program to filter even value
# Function to filter even values
def filtereven(data):
even=[]
for n in data:
if n%2==0:
even.append(n)
return even
# List of fibonacci values
fibo = [0,1,1,2,3,5,8,13,21,34,55]
print("List of fibonacci values :",fibo)
evenFibo = filtereven(fibo)
print("List of even fibonacci values :",evenFibo)
Output:
List of fibonacci values : [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55]
List of even fibonacci values : [0, 2, 8, 34]
Python program to filter even values using lambda function
# Python program to filter even value
# using lambda function
# List of fibonacci values
fibo = [0,1,1,2,3,5,8,13,21,34,55]
print("List of fibonacci values :",fibo)
# filtering even values using lambda function
evenFibo = list(filter(lambda n:n%2==0,fibo))
print("List of even fibonacci values :",evenFibo)
Output:
List of fibonacci values : [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55]
List of even fibonacci values : [0, 2, 8, 34]
Program to filter even values from list using regular function
Output:
Python program to filter even values using lambda function
Output:
need an explanation for this answer? contact us directly to get an explanation for this answer