Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Python find maximum string length in the list of lists of lists

big_list = [[['asdf','ad'],['aqwe','rt']],['lkjyui','op'],['dfgh','hjk']]

Goal is to find the maximum string length in the whole big_list

My approach:

big_list = [[['asdf','ad'],['aqwe','rt']],['lkjyui','op'],['dfgh','hjk']]

listdf= pd.concat(pd.DataFrame(item).T for item in big_list ).reset_index(drop=True)
listdf = 
       0     1
0    asdf  aqwe
1      ad      
2  lkjyui    op
3    dfgh   hjk

print(listdf.astype(str).applymap(lambda x: len(x)).max().max())
6

Is there a better way of doing it?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

With pandas:

out = listdf.stack().str.len().max()

In pure python:

out = max(len(x) for l in big_list for x in l)

Output: 6

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading