import string import random import pprint x = string.ascii_letters + string.digits y = [["".join([random.choice(x) for _ in range(random.randint(1, 100))]) for _ in range(random.randint(1, 10))] for _ in range(50)] def sort_ll(item): item.sort(key = lambda x: len(x), reverse=True) return len(item) y.sort(key=sort_ll, reverse=True) pprint.pprint(y)