[Python] Python : variable type-hinted to to be a dictionary that accept list as value is also...

Discussão em 'Python' iniciado por Stack, Agosto 29, 2025.

  1. Stack

    Stack Membro Participativo

    I am experimenting with type-hinting in python 3.12. I've declared a variable that is type-hinted to be a dictionary whose keys are string and values will be a list of string.

    mydict : dict[str,list[str]] = {
    "d":["k","t","k"]
    }
    #output : mydict ----> d:[k,t,k]


    If I exchange the list value with a set, I get no error. A set is a valid value in place of a list.

    mydict : dict[str,list[str]] = {
    "d":{"k","t","k"}
    }

    output : mydict -----> d : {k,t}


    My question is why I am not getting an error? How to overcome this problem?

    Continue reading...

Compartilhe esta Página