filter() function

Asked 2 years ago, Updated 2 years ago, 351 views

# General function
def even(x):
    if x % 2 == 0:
        return x

# List
two = [i for i in range(10)]

# filter() + general function
list(filter(even, two))

The expected output of is

[0, 2, 4, 6, 8]

The actual output was

[2, 4, 6, 8]

is.
I don't know why 0 isn't being printed here Masters, help me!

python3 filter

2022-11-10 03:13

1 Answers

The even function must be a function that returns true or false.

def even(x):
    return x%2 == 0

The reason why the current even function works roughly is


2022-11-10 03:39

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.