Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to print max size of list inside a list

Tags:

python

For example:

alist=[[(0, 0), (0, 1), (1, 1), (1, 2), (2, 2), (2, 1), (2, 0), (1, 0), (3, 0)], 
[(0, 2), (0, 3), (1, 3)],
[(0, 4), (1, 4), (2, 4), (0, 5), (0, 6), (1, 6), (2, 6), (3, 6)],
[(1, 5)],
[(2, 3), (3, 3), (3, 4), (3, 5), (2, 5), (3, 2), (3, 1)]]

for i in dlist:
    print(len(i))
    print(max(len(i)))  #this gives me error

output:

5
9
3
8
1
7

I wanted to print out 9 as my output from the above list. How am I able to print the result?

like image 985
XtheD Avatar asked Jan 26 '26 06:01

XtheD


2 Answers

Somewhat more terse

len(max(alist,key=len))
like image 152
iruvar Avatar answered Jan 27 '26 22:01

iruvar


If you can be sure your nesting is only one level (that is, a list of lists):

print max(len(sublist) for sublist in alist)
like image 21
Peter DeGlopper Avatar answered Jan 27 '26 21:01

Peter DeGlopper