Python Dictionary Tip!! #python #coding #programming

preview_player
Показать описание
This video shows how to use the get method in Python to access dictionaries and return a default value.

Background Music:
Creative Commons / Attribution 4.0 International (CC BY 4.0)
Рекомендации по теме
Комментарии
Автор

This is one of the ones that I think I'll actually be using! Thank you!

theminecommander
Автор

Every short I find I put it in a good-to-know list. Great info!!

Josue_Estrada
Автор

these python tips actually useful. I haven't use python for my projects, but I know it's useful. Unlike some other youtubers that gave people tips but not useful at all or tips that most people actually have know about

limm
Автор

My favorite way to use this is to return an empty dictionary for the default value. This way you can traverse huge nested dictionaries really easily. For instance, a school curriculum with Course, Section, Class Number that contains a list of required textbooks:

books = curriculum.get("DDSN", {}).get("101", {}).get("001", [])

This returns a list whether or not the class "DDSN 101-001" exists. With much more complex dictionaries this can be incredibly useful.

jumper
Автор

Thank you you told me the difference between x.get() and x[i]

znskrcg
Автор

This is helpful as there are examples 🎉

Budgie-vi
Автор

If you want that default to be inserted into the dictionary instead, you could use a defaultdict

torshepherd
Автор

This is amazing for getting values in fetch request and accounting for empty values. In the pokemon api there are some pokemon that have many types and I was unable to render both types since I would get an error if the second type value was empty now I can assign it an empty string

laxlyfters
Автор

Better practise to my knowledge would be having a Players class that wraps all the relevant information, and then just a list of classes.
Another option would also be importing defaultdict.

MyManJohnny
Автор

Hey, @b001 can you make a video on how to setup vs code for python?

mvreddy
Автор

The future of coding is here. Every time u type code a meme pop out.

xxnotmuchxx
Автор

Do you think this is a better method or an if statement where you would say: if “level” in p.keys():
lvl = p[‘level’]
else:
lvl = 0

I like the one liner of the get method, but is there a reason to use one over the other?

NUHUH
Автор

I am not into python but this should serve the same purpose with more readability :
lvl = p['level'] or 0

abtuly
Автор

Or simply do if 'level' in p.keys()

aggi
Автор

Nice! What theme do you use in VSCode?

kritonios
Автор

Working with non strict data types. Tisk tisk

alext
Автор

Couldnt you just make the line p['level'] or 0? Does .get() do something special?

CrateSauce
Автор

every player gets a default value assigned cause you normally cant afford having a messy database

pantanlp
Автор

Can't python ignore KeyError if you use 'or' keyword?

Is this work in Python?:
level = player['level'] or 0

Becuse you can do this in php with nullsafe (??):
$level = $player['level'] ?? 0;

Or JS with 'or' operator:
let level= player['level'] || 0

elliotphoenix
Автор

Lol, lose with the word database. A list of variable names that are dicts.

louistiches