# [Tutor] max(len(item)) for cols

János Juhász janos.juhasz at VELUX.com
Fri Jun 17 15:16:30 CEST 2005

```Thanks Max,

it is sort, but it gives back the max(len(item)) for the rows

> I have a 2D array:
>
>>>> [['1', '2 ', '3    '], ['longer     ', 'longer    ', 'sort']]
>>>>
>
>
> How can I  get what is the max(len(item)) for the columns ?
> I would like to get a list with the max_col_width values.
>
> I hope that, it can wrote with some nice list comprehension, but I
> can't do
> that :(

Here is the array:

[['1',             '2 ',         '3   '],
[ 'longer    ','longer    ','sort']
]

it is len(item) for that:
[[ 1,   2,  3],
[ 10,10, 4]
]

I would have that is the next:
(10,10,4)
or
[10,10,4]

As I remeneber it is somewhere in numpy, but I am not sure.
I would like to have it simply.

Yours sincerely,
______________________________
János Juhász

Max Noel
<maxnoel_fr at yahoo.fr>
To
János Juhász <janos.juhasz at VELUX.com>
2005.06.17 13:35       cc
tutor at python.org
Subject
Re: [Tutor] max(len(item)) for cols

On Jun 17, 2005, at 11:58, János Juhász wrote:

>
> Dear Guys,
>
> I have a 2D array:
>
>>>> [['1', '2 ', '3    '], ['longer     ', 'longer    ', 'sort']]
>>>>
>
>
> How can I  get what is the max(len(item)) for the columns ?
> I would like to get a list with the max_col_width values.
>
> I hope that, it can wrote with some nice list comprehension, but I
> can't do
> that :(

>>> a = [[1, 2], [1, 2, 3], [1, 2, 3, 4], [1]]
>>> max(len(item) for item in a)
4

If you're running Python 2.3 or lower, add brackets in the last
line:
>>> max([len(item) for item in a])
4

-- Max
maxnoel_fr at yahoo dot fr -- ICQ #85274019
"Look at you hacker... A pathetic creature of meat and bone, panting
and sweating as you run through my corridors... How can you challenge
a perfect, immortal machine?"

```