I have a series of genes that I would like to associate with a list of properties and a color:
gene_A_color = red gene_B_color = blue gene_A_property = ['a','b','c'] gene_B_property = ['d','e','f'] For the purpose of plotting I would like to create a dictionary where I can use the property value as the key to get either the color or the gene as follows:
#lookup dictionary {'a': ['red', 'gene_A'] 'b': ['red', 'gene_A'] 'c': ['red', 'gene_A'] 'd': ['blue' 'gene_B'] 'e': ['blue' 'gene_B'] 'f': ['blue' 'gene_B']} lookup[a][0] = red lookup[a][1] = gene_A I started off as so but can only invert the list if I lose the gene name:
lookup_dict = defaultdict(list) lookup_dict['red'] = ['a','b','c'] lookup_dict['blue'] = ['d','e','f'] inverted = defaultdict(list) for k, v in lookup_dict.items(): inverted[v].append( k ) #inverted {'a': 'red' 'b': 'red' 'c': 'red' 'd': 'blue' 'e': 'blue' 'f': 'blue' } suggestions?