The True Power of "defaultdict" in Python

Поділитися
Вставка
  • Опубліковано 6 лют 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?

  • @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 .-.

  • @eliavrad2845
    @eliavrad2845 Місяць тому +4

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

  • @JohnMitchellCalif
    @JohnMitchellCalif Місяць тому

    super useful! Subscribed

  • @100xBytes
    @100xBytes Місяць тому

    very useful function

  • @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.

  • @ChrisHalden007
    @ChrisHalden007 Місяць тому

    Great video. Thanks

  • @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

  • @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.

  • @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.

  • @worxchi
    @worxchi Місяць тому

    who is this diva

  • @epicmoofish3726
    @epicmoofish3726 Місяць тому +2

    python is actually just 90% bloat

  • @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)());