The reason csv doesn't support that is because variable-length lines are not really supported on most filesystems. What you should do instead is collect all the data in lists, then call zip() on them to transpose them after.
>>> l = [('Result_1', 'Result_2', 'Result_3', 'Result_4'), (1, 2, 3, 4), (5, 6, 7, 8)]
>>> zip(*l)
[('Result_1', 1, 5), ('Result_2', 2, 6), ('Result_3', 3, 7), ('Result_4', 4, 8)]
Answer from Ignacio Vazquez-Abrams on Stack OverflowThe reason csv doesn't support that is because variable-length lines are not really supported on most filesystems. What you should do instead is collect all the data in lists, then call zip() on them to transpose them after.
>>> l = [('Result_1', 'Result_2', 'Result_3', 'Result_4'), (1, 2, 3, 4), (5, 6, 7, 8)]
>>> zip(*l)
[('Result_1', 1, 5), ('Result_2', 2, 6), ('Result_3', 3, 7), ('Result_4', 4, 8)]
wr.writerow(item) #column by column
wr.writerows(item) #row by row
This is quite simple if your goal is just to write the output column by column.
If your item is a list:
yourList = []
with open('yourNewFileName.csv', 'w', ) as myfile:
wr = csv.writer(myfile, quoting=csv.QUOTE_ALL)
for word in yourList:
wr.writerow([word])
Writing a list as a column to a csv file
Put each values on next column of csv file
Use Python to write on specific columns in csv file - Stack Overflow
Writing Python lists to columns in csv - Stack Overflow
Its me again. I have tried every angle my brain will allow me to go, but I'm still stuck. If anyone has some good resources on how to write a list AS A COLUMN to a csv file could you post them here? Thanks
Change them to rows:
rows = zip(list1, list2, list3, list4, list5)
Then just:
import csv
with open(newfilePath, "w") as f:
writer = csv.writer(f)
for row in rows:
writer.writerow(row)
The following code writes python lists into columns in csv
import csv
from itertools import zip_longest
list1 = ['a', 'b', 'c', 'd', 'e']
list2 = ['f', 'g', 'i', 'j']
d = [list1, list2]
export_data = zip_longest(*d, fillvalue = '')
with open('numbers.csv', 'w', encoding="ISO-8859-1", newline='') as myfile:
wr = csv.writer(myfile)
wr.writerow(("List1", "List2"))
wr.writerows(export_data)
myfile.close()
The output looks like this

With Python3, open the file in w mode:
with open('returns.csv', 'w') as f:
writer = csv.writer(f)
for val in daily_returns:
writer.writerow([val])
With Python2.6+, open the file in wb mode:
with open('returns.csv', 'wb') as f:
writer = csv.writer(f)
for val in daily_returns:
writer.writerow([val])
Alternate solution: Assuming daily_returns is the name of the list you wish to write as a column in a CSV file, the following code should work:
with open('return.csv','w') as f:
writer = csv.writer(f)
writer.writerows(zip(daily_returns))