import itertools def koalitionen(ergebnisse: dict, low_threshold: float, high_threshold: float): for n in range(2, len(ergebnisse)): for combo in itertools.combinations(ergebnisse.items(), r=n): if low_threshold <= sum(e[1] for e in combo) <= high_threshold: total = f"{sum(e[1] for e in combo):.1f} %" koalition = " | ".join([e[0] for e in combo]) print(total, koalition, sep=" | ") ergebnisse = { "cdu": 28.5, "afd": 20.6, "spd": 16.4, "grĂ¼ne": 11.9, "linke": 8.6, # "bsw": 4.9, # "fdp": 4.5, # "sonstige": 4.6, } koalitionen( ergebnisse, 50.0, 60, )