Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find max length of each column in a list of lists

Tags:

python

I'm using Python 2.6 along with the xlwt and pyodbc modules to create excel reports from a view on an MS SQL Sever (2008). In order to programatically set the correct width of each field in excel, I would like to retrieve the maximum length of the values in each column.

For example

foo=[[1, 'This is a test',12039],[12, 'test',1235]]

Would result in [2,14,5]

I'm sure there is an easy solution that I'm just overlooking.

like image 544
garnertb Avatar asked May 16 '11 14:05

garnertb


1 Answers

[max(len(str(x)) for x in line) for line in zip(*foo)]
like image 57
Sven Marnach Avatar answered Sep 19 '22 01:09

Sven Marnach