Using 'get()' to Return a Default Value from a Python Dict

preview_player
Показать описание
Here's a common situation working with dictionaries in Python: You want to look up the value of a key and if the key doesn't exist then you want to fall back to a default value.

Рекомендации по теме
Комментарии
Автор

thanks so much, very clear to understand the .get().

qq
Автор

It was nice the way you took us to the climax. It was smooth.

Anonymous-
Автор

I'm a Brazilian and I really enjoyed this video. Nice work!

Автор

Hi Dan, great tip! I like the way you developed the thought process leading to the solution. What python console are you using? I love the code completion feature that I don't have from within terminal or iTerm.

pwood
Автор

Thank you for providing easy and smooth example ..Thanks.

baderalmutairi
Автор

Okay so the last version

def greeting(user_id):
return “Hi %s!” % name_for_user_id.geet(user_id, “there”)

doesn’t have to deal with a ‘membership check’ and you don’t have to write an exception block, and it’s understandable.

Also, String substitution is the name for the weird %s.

ToriKo_
Автор

Hi, you had to finish with setdefault method, but it still cool presentation of the subject. Thanks!

MrNagios
Автор

what if they put %s in the entry box and what even is %s

Throwingwater
Автор

how can you print the key though instead of the value?

konjis
Автор

Why is it not good style to use a membership check?
I'm assuming because every time it executes it has to check through a whole, what might be megabytes or ids for this check .... but that is not really a "style" think is it? It is more of an efficiency thing?

But, even with the key error thing you still end up checking all the keys, no? But at least, not more than once.

Great video by the way, super to the point and efficient! thumbs up

justgivemethetruth
Автор

why isn't the return statement working in sublime text ?
i copied the same code it doesnt gives any output nor any error. Though it runs on python.

arpitj_
Автор

Thanks for the lesson, but i think, f-strings more pretty

GDisclaimer
Автор

How do you use it in nested dictionaries?

carloslara