[Python] python dict select alternative key [duplicate]

Discussão em 'Python' iniciado por Stack, Março 16, 2025.

  1. Stack

    Stack Membro Participativo

    Is there a slick way to select an alternative key if the key does not exist? Dict.get seems to support alternate functionality, but it balks when the alternative is a key.

    >>> d=[{"k":1},[{"k":1}]]
    >>> for i in d:
    ... print(i.get(["k"],[0]["k"]))
    ...
    <stdin>:2: SyntaxWarning: list indices must be integers or slices, not str; perhaps you missed a comma?
    Traceback (most recent call last):
    File "<stdin>", line 2, in <module>
    TypeError: list indices must be integers or slices, not str

    Continue reading...

Compartilhe esta Página