[Python] Is it reasonable to use None as a dictionary key in Python?

Discussão em 'Python' iniciado por Stack, Setembro 5, 2025.

  1. Stack

    Stack Membro Participativo

    None seems to work as a dictionary key, but I am wondering if that will just lead to trouble later. For example, this works:

    >>> x={'a':1, 'b':2, None:3}
    >>> x
    {'a': 1, None: 3, 'b': 2}
    >>> x[None]
    3


    The actual data I am working with is educational standards. Every standard is associated with a content area. Some standards are also associated with content subareas. I would like to make a nested dictionary of the form {contentArea:{contentSubArea:[standards]}}. Some of those contentSubArea keys would be None.

    In particular, I am wondering if this will lead to confusion if I look for a key that does not exist at some point, or something unanticipated like that.

    Continue reading...

Compartilhe esta Página