Question: I need to nicely change a tuple of tuples which looks this way 

```
 a = ((10,1),(10,2),(10,3),(20,21),(30,31),(30,32),(40,41))  

```

to a dict 

```
 b = {10:[1,2,3], 20:[21], 30:[31,32], 40:[41]}

```

The source tuple is the result of an SQL Query ; tuple is sorted according first value of each tuple ; each tuple is a couple.

I don't succeed in coding it a pythonic way ? Can someone help ?

Answer: 
You can use a `defaultdict` here from the collections module:

```py
from collections import defaultdict

b = defaultdict(list)

for k, v in a:
    b[k].append(v)

defaultdict(<class 'list'>, {10: [1, 2, 3], 20: [21], 30: [31, 32], 40: [41]})

```
```
a = list(((10,1),(10,2),(10,3),(20,21),(30,31),(30,32),(40,41)) )

b = {} 
for x, y in a: 
    if x in b: 
        b[x].append(y) 
    else: 
        b[x] = [y] 

# Printing Output 
print(b) 

```