data_set = [ {'Active rate': [0.98, 0.97, 0.96]}, {'Operating Expense': [3.104, 3.102, 3.101]} ]
First, just a quick comment, your original data structure does not necessarily make sense as it is. You use a list of dicts, but each dict seems to use only one key, which seems to be pursuing the goal.
Other data structures that might make more sense would be like this (where each dict structure is used, as it is currently, for one label / value pair, but at least a dict is used to indicate the label and value ):
data_set = [ {'label': 'Active rate', 'values': [0.98, 0.97, 0.96]}, {'label': 'Operating Expense', 'values': [3.104, 3.102, 3.101]} ]
or perhaps better, OrderedDict , which gives you both the order of your original dataset and the benefits of key / value mapping:
from collections import OrderedDict data_set = OrderedDict() data_set['Active rate'] = [0.98, 0.97, 0.96] data_set['Operating Expense'] = [3.104, 3.102, 3.101]
Of course, we do not always choose the data structures that we receive, so suppose you cannot change them. Then your question will become a problem for replacing the roles of rows and columns from the original dataset. In fact, you want to iterate over multiple lists at once, and zip is very useful for this.
import csv fieldnames = [] val_lists = [] for d in data_set:
Note that there is no need to use DictWriter when you are using zip , as that means you need to rebuild the dict without any real benefit.