Just loop over the keys, in sorted order:
for length in sorted(results):
print length, [t.originalWord for t in results[length]]
A defaultdict
is still a dictionary; sorted(results)
returns a list of all the keys in that dictionary, sorted. Using each key you can then access the stored lists of your special objects again, with results[length]
. The list comprehension merely pulls out each originalWord
attribute from those objects.