I have a dictionary with 46,000 key:value pairs where each key has a 3 item list as values:
my dict = {key1: ['A', 'B', 'C'], key2: ['B', 'A', 'G'], key3: ['Z', 'H', 'I']......}
I have a list with hundreds of values:
L1 = ['A', 'A', 'B', 'D', ......]
How do I iterate through the list L1 and for each item in L1, match each dictionary value where value[0] matches the list item? I then wish to perform other operations on value[1] and value[2] of the dictionary only on those key:value pairs where a list item matched value[0] in the dictionary.
In the above example, the first item in L1 - 'A' would match only key1: ['A', 'B', 'C']
.
I can't seem to figure out a way to do this? Thanks for the help!
Without any shortcuts, you could write something like this:
#!/usr/bin/env python
d = {
'key1' : ['A', 'B', 'C'],
'key2' : ['B', 'A', 'G'],
'key3' : ['Z', 'H', 'I']
}
l = ['A', 'A', 'B', 'D']
uniq_l = set(l) # you don't need to check duplicates twice
for key, value in d.items():
if value[0] in uniq_l:
print "Match", key, value
# Output:
# Match key2 ['B', 'A', 'G']
# Match key1 ['A', 'B', 'C']