No video

PRO TIP For Using Dictionaries In Python (ft. get & setdefault)

  Рет қаралды 13,804

Indently

Indently

Күн бұрын

Пікірлер: 25
@lawrencedoliveiro9104
@lawrencedoliveiro9104 Жыл бұрын
I keep forgetting about setdefault. Must remember to use that more. And here’s another tip: deleting a dictionary entry that might not exist. Where del d[key] will raise KeyError if there is no such entry, d.pop(key, None) will not.
@alidev425
@alidev425 Жыл бұрын
Your videos are always so interesting and engaging. Keep up the good work!
@krzysiekkrzysiek9059
@krzysiekkrzysiek9059 Жыл бұрын
More examples like this one please. Short and valuable tips.
@hlubradio2318
@hlubradio2318 2 ай бұрын
Always great thanks
@versacebroccoli7238
@versacebroccoli7238 Жыл бұрын
Yo that .get() method is sick. Before I was using; try: dict[key] except KeyError: pass
@ApplepieFTW
@ApplepieFTW Жыл бұрын
A comparison of setdefault vs defaultdict could be really interesting too
@NoProblem76
@NoProblem76 Жыл бұрын
sigh, it should've been DefaultDict
@piotrrybka318
@piotrrybka318 4 ай бұрын
One important information is that if used with an existing key, `setdefault` will not change its value, so: res = d.setdefault('xyz', 0) # creates 'xyz': 0 print(res) # 0 res = d.setdefault('xyz', 1) # takes already existing 'xyz' print(res) # 0 # not 1
@Hoxle-87
@Hoxle-87 Жыл бұрын
Great for tree searches
@nadavstretiner
@nadavstretiner Жыл бұрын
You could also use defaultdict, one of my favourite collections.
@xBobz99
@xBobz99 Жыл бұрын
So calling setdefault is like dict[key] = dict.get(key, default)
@miguelvasquez9849
@miguelvasquez9849 Жыл бұрын
there is a way to perform an action with the old value and the new with "setdefault" if the key already exists?
@loverboykimi
@loverboykimi Жыл бұрын
that's gold!
@johanngerell
@johanngerell Жыл бұрын
:D and here I've been doing my own "get or create" dict helper function for eons
@Indently
@Indently Жыл бұрын
Tell me about it ahah, these changed my life when I learned about them.
@HL_SAIRA
@HL_SAIRA 2 ай бұрын
Great
@U53RN07F0UND
@U53RN07F0UND Жыл бұрын
Not a method of a built-in, but I find myself using StringIO from the io library an awful lot lately.
@mcdolla7965
@mcdolla7965 Жыл бұрын
i think for this kind of problem , defaultdict() is better sir
@Indently
@Indently Жыл бұрын
They're 2 completely different functions that do 2 completely different things.
@mcdolla7965
@mcdolla7965 Жыл бұрын
yeah...i agree sir, felt like similar thas y commented
@lawrencedoliveiro9104
@lawrencedoliveiro9104 Жыл бұрын
This is why it helps to keep the Python library docs in your bookmark list!
@dragweb7725
@dragweb7725 Жыл бұрын
interesting
@Sinke_100
@Sinke_100 Жыл бұрын
👍🏻
@chx75
@chx75 Жыл бұрын
get is nice, but setdefault is messy
@kvelez
@kvelez 9 ай бұрын
people = {"Mario":1, "Luigi":2} print(people["Mario"]) print(people.get("Wario", -1)) # val/None if not a key print(people.setdefault("Waluigui", 3)) try: print(people['Peach']) except KeyError: print("Not a valid key.") print(people)
AVOID Using: "import *" At ALL Costs In Python
6:29
Indently
Рет қаралды 10 М.
5 Useful Python Decorators (ft. Carberra)
14:34
Indently
Рет қаралды 97 М.
Мы сделали гигантские сухарики!  #большаяеда
00:44
Yum 😋 cotton candy 🍭
00:18
Nadir Show
Рет қаралды 7 МЛН
5 Good Python Habits
17:35
Indently
Рет қаралды 503 М.
10 Ways You Can Use "_" In Python (Do you know ALL of them?)
9:56
Python *ARGS & **KWARGS are awesome! 📦
14:54
Bro Code
Рет қаралды 76 М.
ALL 11 Dictionary Methods In Python EXPLAINED
9:26
Indently
Рет қаралды 57 М.
5 Tips To Write Better Python Functions
15:59
Indently
Рет қаралды 102 М.
10 Important Python Concepts In 20 Minutes
18:49
Indently
Рет қаралды 57 М.
6 CLEAN Tips To IMPROVE Your Python Functions
9:55
Indently
Рет қаралды 77 М.
Python 101: Learn the 5 Must-Know Concepts
20:00
Tech With Tim
Рет қаралды 1,1 МЛН
If __name__ == "__main__" for Python Developers
8:47
Python Simplified
Рет қаралды 397 М.