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
76
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
60
The Command Line Interface
eloy
2
490
Python Decorators
eloy
8
640
Other Decks in Programming
See All in Programming
CSC307 Lecture 03
javiergs
PRO
1
490
Grafana:建立系統全知視角的捷徑
blueswen
0
330
AI Agent の開発と運用を支える Durable Execution #AgentsInProd
izumin5210
7
2.3k
AI時代のキャリアプラン「技術の引力」からの脱出と「問い」へのいざない / tech-gravity
minodriven
20
6.9k
インターン生でもAuth0で認証基盤刷新が出来るのか
taku271
0
190
Vibe Coding - AI 驅動的軟體開發
mickyp100
0
170
AIエージェントのキホンから学ぶ「エージェンティックコーディング」実践入門
masahiro_nishimi
5
330
「ブロックテーマでは再現できない」は本当か?
inc2734
0
750
Data-Centric Kaggle
isax1015
2
760
Smart Handoff/Pickup ガイド - Claude Code セッション管理
yukiigarashi
0
130
CSC307 Lecture 02
javiergs
PRO
1
770
責任感のあるCloudWatchアラームを設計しよう
akihisaikeda
3
160
Featured
See All Featured
How to Think Like a Performance Engineer
csswizardry
28
2.4k
Leo the Paperboy
mayatellez
4
1.4k
WCS-LA-2024
lcolladotor
0
450
Visual Storytelling: How to be a Superhuman Communicator
reverentgeek
2
430
The World Runs on Bad Software
bkeepers
PRO
72
12k
B2B Lead Gen: Tactics, Traps & Triumph
marketingsoph
0
53
Easily Structure & Communicate Ideas using Wireframe
afnizarnur
194
17k
Un-Boring Meetings
codingconduct
0
200
From π to Pie charts
rasagy
0
120
A brief & incomplete history of UX Design for the World Wide Web: 1989–2019
jct
1
300
Mozcon NYC 2025: Stop Losing SEO Traffic
samtorres
0
140
Prompt Engineering for Job Search
mfonobong
0
160
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