How to remove a certain key and value in a dictionary with composite data (Keys and Values) in Python?

Asked

Viewed 93 times

0

I made a composite dictionary (Keys and Values) of alchemists of the anime Fullmetal Alchemist Brotherhood with the following keys: Nickname and patent and wanted to know how to erase the patent key along with its values of all alchemists stored in that dictionary.

alquimistas = {'Edward Elric': {'Alcunha': 'Alquimista de Aço', 'Patente': 'Major'},
               'Alex Louis Armstrong': {'Alcunha': 'Alquimista dos Punhos Poderosos', 'Patente': 'Major'},
               'Roy Mustang': {'Alcunha': 'Alquimista das Chamas', 'Patente': 'Coronel'},
               'Isaac McDougal': {'Alcunha': 'Alquimista de Gelo', 'Patente': 'Major'},
               'Basque Grand': {'Alcunha': 'Alquimista de Sangue e Ferro', 'Patente': 'General de Brigada'},
               'Giolio Comanche': {'Alcunha': 'Alquimista de Prata', 'Patente': 'Major'},
               'Shou Tucker': {'Alcunha': 'Alquimista da Trama Vital', 'Patente': 'Major'},
               'Solf J. Kimblee': {'Alcunha': 'Alquimista Rubro', 'Patente': 'Major'},
               'Tim Marcoh': {'Alcunha': 'Alquimista de Crista', 'Patente': 'Major'}
              }

print (alquimistas)

2 answers

3


You need to iterate over all the elements using for and within the loop use the del to delete a certain key, see:

for nome in alquimistas:
    del alquimistas[nome]['Patente']

See working: https://onlinegdb.com/Hkwg3DOj4

  • That one nome came from where?

  • when you iterate on an object you can extract the indice of each iteration; i.e. nome will always be the name of the alchemist

  • Returns nothing

  • put a code test for you to see the result

  • Missed putting the print (alquimistas) why the code did not return anything.

  • You can remove the patent key of only one alchemist?

  • yes @Alexf. use the del as I showed in the reply: del alquimistas['Edward Elric']['Patente']

  • Would have to put for 'Edward Elric' in alquimistas:
 del alquimistas['Edward Elric']['Patente'] ?

Show 4 more comments

3

First you have to go through all the alchemists, and for each of them, erase the patent key.

With the for you can go through the dictionary.

In the first iteration alquimista will be Edward Elric, afterward Alex Louis Armstrong, afterward Roy Mustang, and so on. For each of these alchemists, you will have to delete the patent key, using the del of python.

for alquimista in alquimistas:
    del alquimistas[alquimista]["Patente"]

After that, if you print in the dictionary again

print (alquimistas)

Will return:

{'Edward Elric': {'Nickname': 'Alchemist of Steel'}, 'Alex Louis Armstrong': {'Nickname': 'Alchemist of the Mighty Fists'}, 'Roy Mustang': {'Nickname': 'Flame Alchemist'}, 'Isaac Mcdougal': {'Nickname': 'Ice Alchemist'}, 'Grand Basque': {'Nickname': 'Alchemist of Blood and Iron'}, 'Giolio Comanche': {'Nickname': 'Silver Alchemist'}, 'Shou Tucker': {'Nickname': 'Alchemist of Trama Vital'}, 'Solf J. Kimblee': {'Nickname': 'Alchemist Rubro'}, 'Tim Marcoh': {'Nickname': 'Crest Alchemist'}}

  • 1

    It worked, thank you.

  • 1

    @Alexf. If one of the answers solved your problem, you can choose the one that best solved it and accept it, see here how and why to do it. It is not mandatory, but it is a good practice of the site, to indicate to future visitors that it solved the problem.

Browser other questions tagged

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