This is trivial with the csv
module:
with open('output.csv', 'w', newline="") as csvfile:
writer = csv.writer(csvfile)
writer.writerows(data)
You already have the header in data
as the first row; you can write all rows in one go with the writer.writerows()
method. That’s all there is to it, really.