I have the list of tuples.
list = [("a", 1), ("b", 2), ("a", 3), ("b", 1), ("a", 2), ("c", 1)]
I want to convert this list of tuples into the dictionary.
Output:
{'a': [1, 3, 2], 'b': [2, 1], 'c': [1]}
My code:
list_a = [("a", 1), ("b", 2), ("a", 3), ("b", 1), ("a", 2), ("c", 1)]
new_dict = {}
value = []
for i in range(len(list_a)):
key = list_a[i][0]
value.append(list_a[i][1])
new_dict[key] = value
print(new_dict)
However, my output is as shown below:
{'a': [1, 2, 3, 1, 2, 1], 'b': [1, 2, 3, 1, 2, 1], 'c': [1, 2, 3, 1, 2, 1]}