#!/usr/bin/python def fairShuffle(a): count = {} for i in a: if count.has_key(i): count[i] += 1 else: count[i] = 1 result = [] # Returns a sorted list of the dictionary's keys, # sorted by values, lowest first: for key in sorted(count, key = count.get): newLength = len(result) + count[key] for i in range(1, count[key] + 1, 1): result.insert((i - 1) * newLength / count[key], key) return result a = ["a" * 5, "b" * 3, "c" * 10, "d", "e"] b = [] for i in a: for u in i: b.append(u) c = fairShuffle(b) print " ".join(c)