from collections import defaultdict d = defaultdict(list) for name in names: key = len(name) d[key].append(name)