Question: I'm creating an Oracle Report and I would like to display certain results based on when I print the report. There is a field called apply\_date. For example, if I print the report <= sysdate of 3/15 then I only want to display the records that have an apply\_date of 3/1 - 3/15. This also goes for printing on the 30th of each month. If I print the report after 3/15 then it would only show me the results that have an apply\_date of >= 3/16.

Answer: 
If you don't care much about the fiddly bits of column alignment, this isn't too bad:

```
datapoints = [{'a': 1, 'b': 2, 'c': 6},
              {'a': 2, 'd': 8, 'p': 10},
              {'c': 9, 'd': 1, 'z': 12}]

# get all the keys ever seen
keys = sorted(set.union(*(set(dp) for dp in datapoints)))

with open("outfile.txt", "wb") as fp:
    # write the header
    fp.write("{}\n".format(' '.join([" "] + keys)))
    # loop over each point, getting the values in order (or 0 if they're absent)
    for i, datapoint in enumerate(datapoints):
        out = '{} {}\n'.format(i, ' '.join(str(datapoint.get(k, 0)) for k in keys))
        fp.write(out)

```

produces

```
  a b c d p z
0 1 2 6 0 0 0
1 2 0 0 8 10 0
2 0 0 9 1 0 12

```

As mentioned in the comments, the [pandas](http://pandas.pydata.org) solution is pretty nice too:

```
>>> import pandas as pd
>>> df = pd.DataFrame(datapoints).fillna(0).astype(int)
>>> df
   a  b  c  d   p   z
0  1  2  6  0   0   0
1  2  0  0  8  10   0
2  0  0  9  1   0  12
>>> df.to_csv("outfile_pd.csv", sep=" ")
>>> !cat outfile_pd.csv
 a b c d p z
0 1 2 6 0 0 0
1 2 0 0 8 10 0
2 0 0 9 1 0 12

```

If you really need the columns nicely aligned, then there are ways to do that too, but I never need them so I don't know much about them.
**Program:**

```
data_points = [
    {'a': 1, 'b': 2, 'c': 6},
    {'a': 2, 'd': 8, 'p': 10},
    {'c': 9, 'd': 1, 'z': 12},
    {'e': 3, 'f': 6, 'g': 3}
]

merged_data_points = {
}

for data_point in data_points:
    for k, v in data_point.items():
        if k not in merged_data_points:
            merged_data_points[k] = []
        merged_data_points[k].append(v)

# print the merged datapoints
print '{'
for k in merged_data_points:
    print '  {0}: {1},'.format(k, merged_data_points[k])
print '}'

```

**Output:**

```
{
  a: [1, 2],
  c: [6, 9],
  b: [2],
  e: [3],
  d: [8, 1],
  g: [3],
  f: [6],
  p: [10],
  z: [12],
}

```