I have a list of lists:
[['a', 1],['b', 2]['c', 3]['c', 1]['c', 9]['d', 8]['d', 4]]
I want to make a dictionary with :
{ a:1, b:2, c:[3,1,9],d:[8,9]}
I have tried the following:
count = 0
sw_fd_dict = {}
value_list =[]
sw_fd_list2 = [['a', 1],['b', 2],['c', 3],['c', 1],['c', 9],['d', 8],['d', 4]]
while count < len(sw_fd_list2):
try:
# to check for repeated keys like "c"
if sw_fd_list2[count][0] != sw_fd_list2[count-1][0]:
sw_fd_dict[sw_fd_list2[count][0]] = sw_fd_list2[count][1]
elif sw_fd_list2[count][0] == sw_fd_list2[count-1][0]:
value_list.append(sw_fd_list2[count-1][1])
value_list.append(sw_fd_list2[count][1])
sw_fd_list2[count][1] =value_list
sw_fd_dict[sw_fd_list2[count][0]] = sw_fd_list2[count][1]
except Exception as e:
print str(e)
count += 1
print sw_fd_dict
Is there a better way to do this?