0

I'm trying to delete all occurrences of elements of C in the dictionary v1.

e.g. in the example below, C = ['L'] and I'm trying to remove L from all the dictionary values (which are lists). I've been working on the code below:

v1 = {'D': ['J', 'K', 'L'], 'B': ['K', 'J', 'L'], 'C': ['L', 'J', 'K'], 'E': ['K', 'L', 'J'], 'A': ['J', 'L', 'K']}
        c = ['L']
        for q,v in v1.items():
            for i in v1.items():
                if c[i] == v[0]:
                    del v[0]

However this has been giving me all sorts of errors: index errors because I'm deleting v[0] and it's iterating over and I'm not sure how to fix this?

unicornication
  • 635
  • 2
  • 7
  • 26

1 Answers1

1
v1 = {'D': ['J', 'K', 'L'], 'B': ['K', 'J', 'L'], 'C': ['L', 'J', 'K'], 'E': ['K', 'L', 'J'], 'A': ['J', 'L', 'K']}
c = ['L']
for q,v in v1.items():
    for i in c:
        v.remove(i)

print v1

Result:

{'A': ['J', 'K'], 'C': ['J', 'K'], 'B': ['K', 'J'], 'E': ['K', 'J'], 'D': ['J', 'K']}
Robᵩ
  • 163,533
  • 20
  • 239
  • 308