Deleting item from list (dictionary) - Python

Asked

Viewed 40 times

-2

I own a dictionary called alunos, that works by relating a student’s name to a list of grades, for example:

alunos = {'Fulano': [2, 4, 10, 5.5]}

I need to delete the first value from this list, having the student’s name as a parameter but I can’t do this operation.

1 answer

0


To remove the first element from a list, use the method remove passing the first value of the list as parameter.

alunos = {'Fulano': [2, 4, 10, 5.5]}
alunos['Fulano'].remove(alunos['Fulano'][0])
print(alunos['Fulano'])

Output:

[4, 10, 5.5]

alunos['Fulano'] refers to the list of grades of the student’s name, alunos['Fulano'][0] refers to the first value of the list.

  • 2

    Another option (which I think is even simpler) is alunos['Fulano'].pop(0)

  • 1

    You can also use del alunos['Fulano'][0]

Browser other questions tagged

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