It works:
lst = ['abc34','def987','ghij','klmno','pqrstuvwxyz1234567','98765','43','210abc']
idx, maxLenStr = max(enumerate(lst), key=lambda x:len(x[1]))
sublist = lst[:idx]
It only iterates through the list once to determine the maximum length, while using max()and then index()repeating it twice across all elements. It also stores the string with the maximum length in maxLenStrand the index where it was found in idx, just in case.
source
share