Question: My function is currently returning:

```
[('a', 'b', 'c'), ('d', 'e', 'f'), ('g', 'e', 'f'), ('h', 'b', 'c')]

```

However, I need the final output to be:

```
[('a', 'h'), ('d', 'g')]

```

As you can see, if i[1] and i[2] match I need i[0] to be paired together. 

I was trying to use a for loop but I can't think of how to write it, at this moment.

Answer: 
I think this solution will preserve order (based on initial match location):

```
from itertools import groupby
from operator import itemgetter
from collections import defaultdict

x = [('a', 'b', 'c'), ('d', 'e', 'f'), ('g', 'e', 'f'), ('h', 'b', 'c')]
groupings, seen_list=defaultdict(list), []

for key, value in groupby(x, itemgetter(1, 2)):
    if key not in seen_list:
         seen_list.append(key)
    groupings[key].extend(list(map(itemgetter(0),value)))

print([groupings[key] for key in seen_list])

```

if order is not important you can disregard the seen\_list and just print the groupings.values()

```
x = [('a', 'b', 'c'), ('d', 'e', 'f'), ('g', 'e', 'f'), ('h', 'b', 'c')]
groupings=defaultdict(list)
for key, value in groupby(x, itemgetter(1, 2)):
    groupings[key].extend(list(map(itemgetter(0),value)))

print(groupings.values())

```
May be not so pythonic, but a bit easier:

```
>>> a = [('a', 'b', 'c'), ('d', 'e', 'f'), ('g', 'e', 'f'), ('h', 'b', 'c')]    
>>> c = {}
>>> [c[j+k].append(i) if j+k in c else c.update({j+k:[i]}) for i,j,k in a]
>>> c = c.values()
>>> print c
[['d', 'g'], ['a', 'h']]

```