Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
The Filter Function
Search
Eloy Zuniga Jr.
May 15, 2018
Programming
0
70
The Filter Function
Basic understanding of the `filter` function.
Eloy Zuniga Jr.
May 15, 2018
Tweet
Share
More Decks by Eloy Zuniga Jr.
See All by Eloy Zuniga Jr.
The `max` function
eloy
0
58
The Command Line Interface
eloy
2
490
Python Decorators
eloy
8
630
Other Decks in Programming
See All in Programming
What’s Fair is FAIR: A Decentralised Future for WordPress Distribution
rmccue
0
160
Java_プロセスのメモリ監視の落とし穴_NMT_で見抜けない_glibc_キャッシュ問題_.pdf
ntt_dsol_java
0
140
Blazing Fast UI Development with Compose Hot Reload (droidcon London 2025)
zsmb
0
500
「正規表現をつくる」をつくる / make "make regex"
makenowjust
1
350
Atomics APIを知る / Understanding Atomics API
ssssota
1
130
Snowflake リリースに注意を払いたくなる話
masaaya
0
110
例外処理を理解して、設計段階からエラーを見つけやすく、起こりにくく #phpconfuk
kajitack
12
5.8k
オンデバイスAIとXcode
ryodeveloper
0
470
CSC509 Lecture 11
javiergs
PRO
0
310
Claude Code on the Web を超える!? Codex Cloud の実践テク5選
sunagaku
0
510
Module Harmony
petamoriken
1
200
DartASTとその活用
sotaatos
2
110
Featured
See All Featured
Keith and Marios Guide to Fast Websites
keithpitt
413
23k
StorybookのUI Testing Handbookを読んだ
zakiyama
31
6.3k
Rebuilding a faster, lazier Slack
samanthasiow
84
9.3k
A Tale of Four Properties
chriscoyier
162
23k
4 Signs Your Business is Dying
shpigford
186
22k
YesSQL, Process and Tooling at Scale
rocio
174
15k
What's in a price? How to price your products and services
michaelherold
246
12k
The Web Performance Landscape in 2024 [PerfNow 2024]
tammyeverts
11
930
Producing Creativity
orderedlist
PRO
348
40k
For a Future-Friendly Web
brad_frost
180
10k
GitHub's CSS Performance
jonrohan
1032
470k
Large-scale JavaScript Application Architecture
addyosmani
514
110k
Transcript
Handle Nonetype objects # Remove Nonetypes before using Max L
= [None, None, None] max([i for i in L if is not None], default=None) @eloy · python 3.6 · 2018 1/14
Using the filter function # Remove Nonetypes before using Max
max(filter(None, L), default=None) @eloy · python 3.6 · 2018 2/14
filter() Keep item if 'True'
Filter function # Takes a function and an iterable filter(<function>,
<iterable>) @eloy · python 3.6 · 2018 4/14
Let's try an example # Anything greater than 2 shall
pass def greater_than_2(item): """Assume item is integer.""" return item > 2 items = filter(greater_than_2, [1, 2, 3, 4]) @eloy · python 3.6 · 2018 5/14
Using the filter function # Remove Nonetypes before using Max
max(filter(None, L), default=None) @eloy · python 3.6 · 2018 6/14
@eloy · python 3.6 · 2018 7/14
Identity function # If NO function is passed, then we
use the "Identity function" filter(None, iterable) # Identity function = Is this Truthy or Falsey? truthy_items = [True, object, 'a', '0', 1, 2, -1] falsey_items = [False, None, '', "", 0] @eloy · python 3.6 · 2018 8/14
Testing identity function # Everything makes it through the filter
everything = filter(None, [True, object, 'a', 1]) # Nothing makes it through the filter nothing = filter(None, [False, None, '', "", 0]) @eloy · python 3.6 · 2018 9/14
Lets make a None function # Anything greater than 2
shall pass def fake_none(item): """Test truthiness.""" return bool(item) items = filter(fake_none, [1, 2, 3, 4]) @eloy · python 3.6 · 2018 10/14
Filter returns an iterable iterable = filter(None, truthy_items) @eloy ·
python 3.6 · 2018 11/14
Still an iterable iterable = list(filter(None, truthy_items)) @eloy · python
3.6 · 2018 12/14
Technically an iterator iterator = filter(None, truthy_items) @eloy · python
3.6 · 2018 13/14
Do you know the difference? 4 iterable 4 lazy iterable
4 iterator 4 range We'll talk about this next month :D @eloy · python 3.6 · 2018 14/14