[Python] Return a default value if a dictionary key is not available

Discussão em 'Python' iniciado por Stack, Maio 28, 2025.

  1. Stack

    Stack Membro Participativo

    I need a way to get a dictionary value if its key exists, or simply return None, if it does not.

    However, Python raises a KeyError exception if you search for a key that does not exist. I know that I can check for the key, but I am looking for something more explicit. Is there a way to just return None if the key does not exist?


    See also: Why dict.get(key) instead of dict[key]?

    Continue reading...

Compartilhe esta Página