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
58
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
52
The Command Line Interface
eloy
2
470
Python Decorators
eloy
8
620
Other Decks in Programming
See All in Programming
いりゃあせ、PHPカンファレンス名古屋2025 / Welcome to PHP Conference Nagoya 2025
ttskch
1
180
ChatGPT とつくる PHP で OS 実装
memory1994
PRO
3
190
PHPUnitしか使ってこなかった 一般PHPerがPestに乗り換えた実録
mashirou1234
0
420
PHPで作るWebSocketサーバー ~リアクティブなアプリケーションを知るために~ / WebSocket Server in PHP - To know reactive applications
seike460
PRO
2
770
AWS re:Invent 2024個人的まとめ
satoshi256kbyte
0
100
KMP와 kotlinx.rpc로 서버와 클라이언트 동기화
kwakeuijin
0
300
Amazon Nova Reelの可能性
hideg
0
200
AHC041解説
terryu16
0
380
Внедряем бюджетирование, или Как сделать хорошо?
lamodatech
0
940
traP の部内 ISUCON とそれを支えるポータル / PISCON Portal
ikura_hamu
0
180
各クラウドサービスにおける.NETの対応と見解
ymd65536
0
250
為你自己學 Python
eddie
0
520
Featured
See All Featured
Site-Speed That Sticks
csswizardry
3
270
4 Signs Your Business is Dying
shpigford
182
22k
Producing Creativity
orderedlist
PRO
343
39k
Improving Core Web Vitals using Speculation Rules API
sergeychernyshev
3
180
jQuery: Nuts, Bolts and Bling
dougneiner
62
7.6k
The MySQL Ecosystem @ GitHub 2015
samlambert
250
12k
Dealing with People You Can't Stand - Big Design 2015
cassininazir
365
25k
RailsConf 2023
tenderlove
29
970
How to Create Impact in a Changing Tech Landscape [PerfNow 2023]
tammyeverts
49
2.2k
Gamification - CAS2011
davidbonilla
80
5.1k
Fontdeck: Realign not Redesign
paulrobertlloyd
82
5.3k
Optimising Largest Contentful Paint
csswizardry
33
3k
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