Использование min( iterable, key=len)
( мин / макс ) является самым простым.Если вы хотите просмотреть свой список самостоятельно, вы можете сделать это следующим образом:
tl = [[1, 2, 3], [1, 2, 3, 4], [1,2,3,4,5]]
# init shortest and longest to 1st element of list
# use a tuple to store: ( listdata, length, position in original list )
shortest = longest = (tl[0], len(tl[0]), 0)
# iterate over remaining elements
for pos_act,act in enumerate(tl[1:],1): # start enumerate on 1 to fix position due slicing
# get actual length
len_act = len(act)
# is it larger then current largest?
if len_act > longest[1]:
longest = (act,len_act, pos_act)
# is it shorter then current shortest?
if len_act < shortest[1]:
shortest = (act, len_act, pos_act)
print ("Shortest: {} with len {} on position {}".format(*shortest))
print ("Longest: {} with len {} on position {}".format(*longest))
Вывод:
Shortest: [1, 2, 3] with len 3 on position 0
Longest: [1, 2, 3, 4, 5] with len 5 on position 2
Доку: