with open("input.txt","r") as f: groups = f.read().split('\n\n') sets = [list(map(set, group.split('\n'))) for group in groups] reduce = lambda fn: sum(len(fn(*s)) for s in sets) print(reduce(set.union), reduce(set.intersection))