Manipulating dictionaries within other dictionaries

Asked

Viewed 222 times

-1

Alunos = {
    111: {
        'nome' : 'Joao',  
        'curso' : 'ADS'
    },

    222: { 
        'nome' : 'Pedro',
        'curso' : 'SI'
    },

    333: { 
        'nome' : 'Maria',
        'curso' : 'SI'
    }
}

I need to write a script to show the names of the students whose course is SI. I’m not getting logic and the teacher didn’t teach it, I already looked on the internet and I couldn’t get anything to give a light.

1 answer

1


Simply filter the dictionary values based on the course, getting the name of each student:

nomes = (aluno['nome'] for aluno in Alunos.values() if aluno['curso'] == 'SI')

And to display the names:

for nome in nomes:
    print(nome)

That would display:

Pedro
Maria

See working on Repl.it | Ideone | Github GIST

Browser other questions tagged

You are not signed in. Login or sign up in order to post.