

0 / 2 embers
0 / 3000 xp
click for more info
Complete a lesson to start your streak
click for more info
Still calibrating
click for more info
Not enough gems
Cost: 6 gems
1: Functions As Values
incomplete
2: Anonymous Functions
incomplete
3: First-Class and Higher-Order Functions
incomplete
4: Map
incomplete
5: Filter
incomplete
6: Reduce
incomplete
7: Map, Filter, and Reduce Review
incomplete
8: Zip
incomplete
9: Higher-Order Functions Practice
incomplete
Back
ctrl+,
Next
ctrl+.
This lesson's interactive features are locked, please to keep using them
The built-in filter function takes a function and an iterable (often a list) and returns an iterator that keeps elements from the original iterable only where the result of the function on that item returned True.
In Python:
def is_even(x: int) -> bool:
return x % 2 == 0
numbers: list[int] = [1, 2, 3, 4, 5, 6]
evens: list[int] = list(filter(is_even, numbers))
print(evens)
# [2, 4, 6]
Complete the remove_invalid_lines function. It accepts a document string as input. It should:
For example, this:
* Star Wars episode 1 is underrated
- Star Wars episode 9 is fine
* Star Wars episode 3 is the best
Should become:
* Star Wars episode 1 is underrated
* Star Wars episode 3 is the best
The following methods may be useful:
"\n".join(["a", "b", "c"])
# a
# b
# c
s: str = "hello"
s.startswith("h")
# True
s.startswith("o")
# False
s: str = """hello
world"""
lines: list[str] = s.split("\n")
# ['hello', 'world']
If the string starts or ends with "\n", .split("\n") includes empty strings at the edges. Joining the pieces with "\n" preserves those newlines.