1
I am trying to store dictionaries in a list and then print according to the position in the list, however this giving an error in the key... Code below:
nome = "nomedahora"
cpf = "1421241"
departamento = "bsi"
dicProfessor = {nome: {"nome":nome, "cpf":cpf, "departamento":departamento}}
listaProfessores = []
listaProfessores.append(dicProfessor)
dicProfessor.clear()
for item in listaProfessores:
print(item[nome])
Error:
Traceback (Most recent call last): File "C:/Users/Student/Appdata/Local/Programs/Python/Python36/rqwrqrqrqr.py", line 18, in print(item[name]) Keyerror: 'namesake'
Make the dicProfessor.clear() call after you finish iterating the list and not before, doing before vc will remove its reference from the list.
– Diego Schmidt