The True Power of "defaultdict" in Python

แชร์
ฝัง
  • เผยแพร่เมื่อ 8 ก.พ. 2025
  • In today's video we're going to be learning how we can use defaultdict in Python.
    ▶ Become job-ready with Python:
    www.indently.io
    ▶ Follow me on Instagram:
    / indentlyreels

ความคิดเห็น • 50

  • @ilikeoreos9701
    @ilikeoreos9701 หลายเดือนก่อน +31

    defaultdict mentioned!! 🔊🔊🔊

  • @foxypiratecove37350
    @foxypiratecove37350 หลายเดือนก่อน +25

    I never thought I would hear Indently saying "yo mamma"

    • @arnowinter462
      @arnowinter462 หลายเดือนก่อน

      Zappa: "Maybe you should stay with yo' mamma" :-)

  • @JustThatRandomTroll
    @JustThatRandomTroll หลายเดือนก่อน +26

    dict.setdefault: «am I a joke to you?»

    • @DrDeuteron
      @DrDeuteron หลายเดือนก่อน +8

      that's a different default.
      except at 6:15 where it is probably preferable.

    • @anon_y_mousse
      @anon_y_mousse หลายเดือนก่อน

      @@DrDeuteron What is the difference between them? Can you still assign a lambda for setdefault or is defaultdict the only way to do that? Also, if you're just using a default constructor of some kind, will there actually be a difference?

  • @eliavrad2845
    @eliavrad2845 หลายเดือนก่อน +4

    def Tree():
    return defaultdict(Tree)
    t = Tree()
    t['level1']['level2']['level3'] = 'a'

  • @swolekhine
    @swolekhine หลายเดือนก่อน +3

    defaultdict is so useful, I've gotten annoyed at some other languages when their version of a dictionary didn't have an equivalent.

    • @DrDeuteron
      @DrDeuteron หลายเดือนก่อน

      what default function do you use? And don't say "int".

    • @omarjimenezromero3463
      @omarjimenezromero3463 หลายเดือนก่อน

      @@DrDeuteron i use int in C++ instead of void .-.

  • @JohnMitchellCalif
    @JohnMitchellCalif หลายเดือนก่อน

    super useful! Subscribed

  • @watchmakerful
    @watchmakerful หลายเดือนก่อน +3

    If you try to access a key without adding it to this dictionary, is that key implicitly added (with the default value) or not?

  • @foxypiratecove37350
    @foxypiratecove37350 หลายเดือนก่อน +9

    4:41 Isn't the f-string overkill here?

    • @Andrumen01
      @Andrumen01 หลายเดือนก่อน +1

      Not necessarily, assume you have a list of unknown values whose types are not necessarily strings. This more general approach considers the 'str' representation of the object...Python is very intelligent (even if slow! Python has this important tradeoff!).

    • @MrMaxtng
      @MrMaxtng หลายเดือนก่อน +2

      Yes it is, upper is a string function. The function does not exist for types like numbers, for example. If you are unsure of the type, you'd do str(string).upper()
      Wrapping that expression with colons is irrelevant. An exception will be raised before that if string is for example an int as upper() is only defined for string types

    • @foxypiratecove37350
      @foxypiratecove37350 หลายเดือนก่อน +1

      ​@@Andrumen01You're wrong, for two reasons: the type annotations indicate that these are strings, and `.upper()` is a method of the `str` class. Also, the code in the video is converting the string in uppercase to a string, so the f-string here is useless.

    • @korntut
      @korntut หลายเดือนก่อน +2

      Isn't using a defaultdict overkill in the string example? A normal dict would already work. Bad example...

    • @foxypiratecove37350
      @foxypiratecove37350 หลายเดือนก่อน

      @korntut I honestly don't know, I'm not a `defaultdict` expert. But yeah it seem a bit overkill, but the f-string was more obvious.

  • @100xBytes
    @100xBytes หลายเดือนก่อน

    very useful function

  • @ChrisHalden007
    @ChrisHalden007 หลายเดือนก่อน

    Great video. Thanks

  • @SimonChang-t7e
    @SimonChang-t7e หลายเดือนก่อน

    Hi there - one thing I use dictionary defaults (I usually use the get function of.a regular function) is for a translation from one representation to another. Often only a few words need to be translated - so the default is the key. For example "american_words={'colour':'color','honour':'honor'} then "new_word = american_words.get(word, word)". Is there a way to do this with default_dict?

  • @TheJaguar1983
    @TheJaguar1983 หลายเดือนก่อน

    defaultdict is so invaluable. It's not uncommon for me to subclass defaultdict to override __missingkey__ so that I can use the key in the creation of the default value.

  • @TheJaguar1983
    @TheJaguar1983 หลายเดือนก่อน

    2:04 That's not actually a cast, it's creating a new dict from the defaultdict

  • @ChalcolithicPrizim
    @ChalcolithicPrizim หลายเดือนก่อน +1

    The true power of defaultdict is allowing you to circumvent your professor’s “No dictionaries” restriction, since it is not a dict = {} 😆

    • @__christopher__
      @__christopher__ หลายเดือนก่อน

      >>> isinstance(defaultdict(list), dict)
      True

  • @sseymour1978
    @sseymour1978 หลายเดือนก่อน

    Calamansi (whatever that is - even spell checked did not know it) example works very well with dict() instead of defaultdict()

  • @TRADINGPULSE397
    @TRADINGPULSE397 หลายเดือนก่อน

    Python flet framework project convert to android apk plzz provide tutorial

  • @MapsCharts
    @MapsCharts หลายเดือนก่อน +2

    C'est horrible la voix IA dégueu pour le doublage

    • @Indently
      @Indently  หลายเดือนก่อน

      Je vais essayer à l’écouter quand je rentre chez moi. Je suis très curieux!

    • @MathisRoyale
      @MathisRoyale หลายเดือนก่อน +2

      La voix originale est bien mieux

  • @n0150
    @n0150 หลายเดือนก่อน +1

    holy crap auto translated ai sounds bad

    • @n0150
      @n0150 หลายเดือนก่อน +1

      like the german version straight up calls python "pithon"
      i mean i appreciate translations, but i prefer the original

  • @DrDeuteron
    @DrDeuteron หลายเดือนก่อน

    5:00
    dict(zip(los, map(str.upper, los))
    is mo' better.

  • @epicmoofish3726
    @epicmoofish3726 หลายเดือนก่อน +2

    python is actually just 90% bloat

  • @worxchi
    @worxchi หลายเดือนก่อน

    who is this diva

  • @davidmurphy563
    @davidmurphy563 หลายเดือนก่อน +2

    Ugh!: interjection All: pronoun these: adverb types: noun make: verb it: pronoun so: adverb hard: adverb to: particle read: verb

    • @foxypiratecove37350
      @foxypiratecove37350 หลายเดือนก่อน

      @@davidmurphy563 Really? I introduce you to statically typed languages, then, even "worst", you're forced to put the complete type for each variable declaration!

    • @Dimitrius1991
      @Dimitrius1991 หลายเดือนก่อน

      @foxypiratecove37350 In that case type declarations are usually separate and don't harm readability.

    • @foxypiratecove37350
      @foxypiratecove37350 หลายเดือนก่อน +1

      @Dimitrius1991 Not really, it's always near the variable's name. Also it's more readable `var: Type = val` as "`var` of type `Type` which is equal to `val`", instead of `Type var = val;` that you or have to reverse the order of the type and the name, or have to say it in an unusual order: "a variable of type `Type` named `var` which is equal to `val`". But at the end, it's you that chose.

    • @Dimitrius1991
      @Dimitrius1991 หลายเดือนก่อน

      @foxypiratecove37350 I mean, it's more readable when a declaration and the corresponding assignment are not in the same line, especially if the type name is long. Also, I've had enough of static typing already.😆

    • @__christopher__
      @__christopher__ หลายเดือนก่อน

      @@Dimitrius1991 Not true. I wish C and C++ type declarations were as well separated as Python ones. Try to decipher what the following declares in C:
      void(**f)(void *(*g)());