[Python] Python nested dictionary lookup with default values

Discussão em 'Python' iniciado por Stack, Julho 20, 2025.

  1. Stack

    Stack Membro Participativo

    >>> d2
    {'egg': 3, 'ham': {'grill': 4, 'fry': 6, 'bake': 5}, 'spam': 2}
    >>> d2.get('spamx',99)
    99
    >>> d2.get('ham')['fry']
    6


    I want to get value of fry inside of ham, if not, get value, 99 or 88 as the 2nd example. But how?

    Continue reading...

Compartilhe esta Página