I am starting with a list of tuples (a,all b). I want to end with a list of tuples (b,all a).

For example:

```
FROM
(a1,[b1,b2,b3])
(a2,[b2])
(a3,[b1,b2])
TO
(b1,[a1,a3])
(b2[a1,a2,a3])
(b3,[a1]
```

How do I do this using Python 2? Thank you for your help.

# Best How To :

You can use `collections.defaultdict`

:

```
tups = [
('a1',['b1','b2','b3']),
('a2',['b2']),
('a3',['b1','b2'])
]
d = collections.defaultdict(list)
for a, bs in tups:
for b in bs:
d[b].append(a)
```

Then:

```
>>> d.items()
[('b1', ['a1', 'a3']), ('b2', ['a1', 'a2', 'a3']), ('b3', ['a1'])]
```