You would be better served with a nested dictionary:
rates={'Alabama':{'Single rate':['0.02', '0.04', '5'],
'Single bracket': ['500', '3000'],
'Couple rate': ['0.02', '0.04', '0.05'],
'Couple bracket': ['1000', '6000']}}
print(rates['Alabama']['Couple rate'])
# ['0.02', '0.04', '0.05']
Assuming your cdv file looks like this:
'Alabama', 'Single rate', '0.02', '0.04', '5'
'Alabama', 'Single bracket', '500', '3000'
'Alabama', 'Couple rate', '0.02', '0.04', '0.05'
'Alabama', 'Couple bracket', '1000', '6000'
You can construct the nested dict this way:
import csv
rates={}
with open(ur_file) as f:
for line in csv.reader(f, skipinitialspace=True, quotechar="'"):
rates.setdefault(line[0],{})[line[1]]=[float(e) for e in line[2:]]
print(rates)
Prints:
{'Alabama': {'Couple rate': [0.02, 0.04, 0.05],
'Single rate': [0.02, 0.04, 5.0],
'Single bracket': [500.0, 3000.0],
'Couple bracket': [1000.0, 6000.0]}}
Edit
As pointed out in the comments, a three tier nested dict is probably better, like this data structure:
rates={'Alabama':{'Single': {'rate':['0.02', '0.04', '5'],
'bracket': ['500', '3000']},
'Couple': {'rate': ['0.02', '0.04', '0.05'],
'bracket': ['1000', '6000']}}}
While it is trivial to use defaultdict or setdefault to deal with a two tier dict with missing keys, it takes a little more though to deal with multiple levels elegantly.
My favorite is to use a Perl like autovivification subclass a dict like so:
class AutoVivify(dict):
"""Implementation of perl's autovivification feature."""
def __missing__(self, item):
value = self[item] = type(self)()
return value
rates=AutoVivify()
with open(ur_file) as f:
for line in csv.reader(f, skipinitialspace=True, quotechar="'"):
state=line[0]
k1,k2=line[1].split()
rates[state][k1][k2]=[float(e) for e in line[2:]]
print(rates)
Prints:
{'Alabama': {'Single': {
'rate': [0.02, 0.04, 5.0],
'bracket': [500.0, 3000.0]},
'Couple': {
'rate': [0.02, 0.04, 0.05],
'bracket': [1000.0, 6000.0]}}}