def add_related_donations(donation): global donations, repeats, results def check(index): if not repeats[index]: return False other = donations[index] return donation.cmte_id == other.cmte_id and \ donation.zip_code == other.zip_code and \ donation.transaction_date.year == other.transaction_date.year new_result = Result(cmte_id=donation.cmte_id, zip_code=donation.zip_code, year=donation.transaction_date.year) for index in range(len(donations)): if check(index): new_result.add(donations[index].transaction_amount) new_result.done(percentile) results.append(new_result)