Sorry if this has already been mentioned somewhere (I could not find it).
I basically want to list an item from a list, but include quotes and brackets (which I don't want). Here is my details:
inputData = {'red':3, 'blue':1, 'green':2, 'organge':5}
Here is my class for finding items based on key or value.
class Lookup(dict): """ a dictionary which can lookup value by key, or keys by value """ def __init__(self, items=[]): """items can be a list of pair_lists or a dictionary""" dict.__init__(self, items) def get_key(self, value): """find the key(s) as a list given a value""" return [item[0] for item in self.items() if item[1] == value] def get_value(self, key): """find the value given a key""" return self[key]
it works fine except for the brackets.
print Lookup().get_key(2)
I know I can do this by replacing the brackets / quotes ( LookupVariable.replace("'", "") ), but I was wondering if there is an even more pythonic way to do this.
Thanks.
source share