Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check max length of array element in Python

I want to check the max length of array elements. Although I can do it with simple code, is there another smart way to implement this in Python 3?

a = [[1,2], [1], [2,2,3,3], [2,2]]
max_len = 0
for d in a:
    max_len = len(d) if len(d) > max_len else max_len
print(max_len)
like image 447
jef Avatar asked Dec 03 '22 13:12

jef


1 Answers

You can do something like this:

max_len = max([len(i) for i in a])
print(max_len)
like image 191
Mathias711 Avatar answered Dec 29 '22 00:12

Mathias711