Write a program that categorizes tuple values based on second element and prints a dictionary value list where each key is a category.


Topic: Write a program that categorizes tuple values based on second element and prints a dictionary value list where each key is a category.

Solution

test_list = [(1, 3), (1, 4), (2, 3), (3, 2), (5, 3), (6, 4)] 
res = {} 
for i, j in test_list: 
     res.setdefault(j, []).append(i) 
print("The dictionary converted from tuple list : " + str(res))



List all Python Programs