from pprint import pprint def load(filename): with open(filename, "r", encoding="utf-8") as f: rec = [] for line in f: s = line.rstrip() if not s: yield rec rec = [] continue rec.append(s) yield rec data = list(load("data.txt")) # Anyone answered yes nq = sum([len(set(c for r in rec for c in r)) for rec in data]) print(f"Sum of counts: {nq}") # Everyone answered yes a = 0 for grp in data: resp = {} for q in grp: for c in q: if c not in resp: resp[c] = 0 resp[c] += 1 n = sum(1 for k, v in resp.items() if len(grp) == v) a += n print(f"Sum of everyone: {a}")