You've Been Accessing Values WRONG From Dictionaries In Python

preview_player
Показать описание
You've been accessing values wrong from dictionaries in Python. #Code #Python #Shorts
Рекомендации по теме
Комментарии
Автор

It's misleading to say that using the dict[key] is wrong. Most of the time, you want to get an exception for doing something wrong in the code. So no, people should not use dict.get UNLESS they really need to check if an element is in the dict

Yvant
Автор

its not always good to avoid exceptions by hiding them like that

vinicus
Автор

tell me you just want attenttion on your videos without telling me you want attenttion on ur videos *misinfornation click bait*

SirXtC
Автор

second positional argument in .get() is None by default, you don't need to specify it explicitly.

also there is nothing wrong with getting dict values using [ ] notation for values that I know are exists and I would use .get() if I'm not sure they are, like in some API calls.

mjaysmileofficial
Автор

Worst advice you can get. Silent errors are the worst errors. Unless there's a reasonable fallback value, you shouldn't do this.

yawninglion
Автор

This isn't necessarily better. If a required key is missing, frameworks like Flask would be much safer with an exception than a None value.

ShrootBuck
Автор

The biggest mistake a programmer can make is thinking that errors are bad

richcaputo
Автор

CLICKBAIT . Sometimes you want a default value, other times you want an exception raised.

bthrkay
Автор

In Java, it’s
map.getOrDefault(someKey, 0); // 0 is the default value.

AbhishekGhosh-ybnl
Автор

even though this is showing a different way of accessing items inside of a dictionary, this one short has helped me understand dictionaries so much better as i always so lost on how they worked, but just seeing this syntax made me realise that its actually quite simple, and that ive just been overthinking it lol, im still very new in my journey of learning python (or just to code in general) so its all an experience! tysm for this short

jaidenh
Автор

Why is first method wrong? This dude seems to be a beginner in programming, who doesn't know power of exceptions.

cipherxen
Автор

None is already the default value by default (sounds funny), so if you don't pass a second argument to dict.get() it will just return None in case it finds nothing

bettercalldelta
Автор

Well thats convenient for API calls. I could remove the try-except error handling and shorten the code. But I still think that error handling is a better approach since you can 'handle' the way you want to proceed after you didn't find what you were looking for.

masterdementer
Автор

This does not really work with python because then you will need to check that case anyway. This works better in languages like rust.

gigachad
Автор

Your conception of what the square bracket notation is used for is wrong and sharing your opinion as fact is questionable.

deemon
Автор

Bro just forgot that try except else finally exists

rodrigoqteixeira
Автор

Using square brackets only need to type 2 character

VietnameseBoii
Автор

It is more pythonic to try except a key error than using.get

chrisvinciguerra
Автор

video starts, I see the code style, I leave.

YamiSuzume
Автор

It's not wrong. It depends on the case. Both ways are useful for different cases.

claffert