You can sort a list of lists by length as follows:
l1 = [1,2,3]
l2 = [1,2,3]
l3 = [1,2]
lists = [l1, l2, l3]
sorted_lists = sorted(lists, key=len)
print sorted_lists #[[1,2], [1,2,3], [1,2,3]]
I can't figure out how to keep track of the indicies to then match up the contents of sorted_lists
with the original list names l1
, l2
and l3
.
This gets close, but I'm not sure how the solution can be implemented when sorting by length.