Remove items from lists in the dictionary

0

I have the following code which is a dictionary with lists inside. What I want to do is erase a certain element from all the lists.

For example: delete everything in position 2 of each list (bike, 2 and C).

dicc={}
dicc['lista1']=['coche', 'moto', 'bici', 'avion', 'barco', 'patin']
dicc['lista2']=['0', '1', '2', '3', '4', '5']
dicc['lista3']=['A', 'B', 'C', 'D', 'E', 'F']

Greetings and thanks

    
asked by Tercuato 02.02.2018 в 11:42
source

2 answers

1

As you said @FJSevilla, you can iterate over your dictionary and remove the elements of position 2.

dicc={}
dicc['lista1']=['coche', 'moto', 'bici', 'avion', 'barco', 'patin']
dicc['lista2']=['0', '1', '2', '3', '4', '5']
dicc['lista3']=['A', 'B', 'C', 'D', 'E', 'F']
INDICE = 2
for i in dicc:
    if len(dicc[i]) < INDICE + 1:
        continue
    dicc[i].pop(INDICE)

print(dicc)
    
answered by 02.02.2018 в 20:23
0

As previously commented, if a certain element is a position within the list you can do:

 elemento = 2 # Indica el indice
 for k, v in b.items():
    if len(v) > elemento:
        v.pop(elemento)

If a certain element is a value contained in the lists:

elemento = "bici" # Indica el valor
for k, v in b.items():
    if elemento in v:
        v.remove(elemento)
    
answered by 23.05.2018 в 01:46