Skip to content

Instantly share code, notes, and snippets.

@vlad-bezden
Last active February 22, 2020 15:47
Show Gist options
  • Save vlad-bezden/049d2b8223d2802acfdb60398eeafb26 to your computer and use it in GitHub Desktop.
Save vlad-bezden/049d2b8223d2802acfdb60398eeafb26 to your computer and use it in GitHub Desktop.
Remove more than one element from the iterable
"""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