Last active
February 22, 2020 15:47
-
-
Save vlad-bezden/049d2b8223d2802acfdb60398eeafb26 to your computer and use it in GitHub Desktop.
Remove more than one element from the iterable
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""Remove more than one element from the iterable""" | |
import random | |
from itertools import filterfalse | |
random.seed(42) | |
data = [random.randrange(5) for _ in range(10)] | |
clean = [*filterfalse(lambda i: i == 0, data)] | |
print(f"Remove 0s\n{data=}\n{clean=}\n") | |
clean = [*filterfalse(lambda i: i in (0, 1), data)] | |
print(f"Remove 0s and 1s\n{data=}\n{clean=}") | |
""" | |
Remove 0s | |
data=[0, 0, 2, 1, 1, 1, 0, 4, 0, 4] | |
clean=[2, 1, 1, 1, 4, 4] | |
Remove 0s and 1s | |
data=[0, 0, 2, 1, 1, 1, 0, 4, 0, 4] | |
clean=[2, 4, 4] | |
""" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment