Question: I have a large dict: 600 keys with items = big list (about 10000-20000 elements).

My goal is to get pairs from each list in dict and merge it in one list. 

E.g. i have:

```
d1 = {'key1': ['a', 'b', 'c', 'd'], 'key2': ['f', 'a']}

```

Expected result: 

```
d2 = ['a_b', 'a_c', 'a_d', 'b_c', 'b_d', 'c_d', 'a_f']

```

My code: 

```
d2 = [] 
for k, v in d1.items():
    for i, j in itertools.product(v, v):
        if i>j:
            a = "_".join(list(set([i, j])))
            d2.append(a)

```

And I have a problem: in terminal my python script says 'Killed'.

This is probably due to inappropriate memory usage. Are there ways to solve this problem?

Answer: 
You could do something like this:

```
import itertools as it
for l in d1.values():
    for t in it.combinations(sorted(l), 2):
        print("_".join(t))

```

Displaying:

```
a_b
a_c
a_d
b_c
b_d
c_d
a_f

```

Note: If you do not want it to be sorted, simply take out the `sorted` function call.
You could create a generator that doesn't involve `itertools`:

```
def dic_comb_generator(d):
    for val in d.values():
        v = sorted(val)
        for i in range(len(v)):
            for j in range(i+1, len(v)):
                yield v[i] + '_' + v[j]

```