Question: I have performed a bunch of pairwise comparisons (241 x 241 to be exact).  

The resulting file looks like this:

```
A,X,10
A,Y,20
X,Y,15

```

I would like to convert this to a table showing me all the pairwise comparisons. I.e. something like this

```
,A,X,Y
A,,10,20
X,10,,15
Y,20,15,,

```

I have no clue how to even begin tackling this problem. Any help, suggestions will be greatly appreciated!

Answer: 
Store your paired data into a dict:

```
result_dict = {
    (A, X): 10,
    (A, Y): 20,
    (X, Y): 15,
}

```

and row/column labels:

```
cols = sorted(set(a for a, b in result_dict.iterkeys()))
rows = sorted(set(b for a, b in result_dict.iterkeys()))

```

then it's how you gonna print rows...

```
for b in rows:
    row = list(result_dict.get((a, b), None) for a in cols)
    # print the row

```
I have a feeling there are more efficient ways, but you could give something like this a shot. It uses the `csv` module to load/parse your data and then writes it back to `csv` as well (assuming you want the output in a file, that is - if not, this can be adjusted):

```
import csv
from collections import defaultdict

# Open the input file and read it all into a defaultdict
with open('table.csv', 'rb') as f:
  reader = csv.reader(f)

  # Dictionary to hold the nested values
  # This will be keyed by the letter ID, and each line of
  # the file will be written twice (so A,X,10 will be represented
  # as {'A': {'X': 10}, 'X': {'A': 10}}
  d = defaultdict(dict)
  for row in reader:
    d[row[0]][row[1]] = row[2]
    d[row[1]][row[0]] = row[2]

# Now we open the output file and write out our defaultdict
with open('t_out.csv', 'wb') as o:
  # Here our fieldnames will start with the 'empty' first header
  # and then be comprised of the keys of the dictionary (which
  # should contain all possible values for the table)
  fieldnames = [' '] + d.keys()
  writer = csv.DictWriter(o, fieldnames=fieldnames)

  # In Python 2.7, you can use writer.writeheaders()
  writer.writerow(dict((h, h) for h in fieldnames))

  # Now iterate through our dictionary, creating a row
  # dictionary that will contain the information to be written
  for k, v in d.iteritems():
    # Here we are putting the key in the 'empty' first column
    v[' '] = k
    writer.writerow(v)

```