Is there any way i can compare both lists and sum the second value ONLY if the the first value matches? I queried two tables and gave me the results below but not sure if python has some sort of group function i can use to group the account number(first value) and sum the second values if the first value matches.
import psycopg2
def connect():
conn = psycopg2.connect(host="test", port=5432, database="test", user="test",
password="test123")
cursor = conn.cursor()
return cursor
def allocation(cursor,cashdt, sumbmaster):
alloclist = []
print("Allocation: ")
cursor.execute("""select bill_acct,sum(debit_amount) from test.a where
cash_date ='"""
+ cashdt + "'" + "and master_bill='" + sumbmaster +"' group by bill_acct")
for row in cursor.fetchall():
a = row[0],"{:.2f}".format(row[1])
alloclist.append(a)
print(alloclist)
def statement(cursor, billingdt,sumbmaster):
statementlist = []
cursor.execute(
"""
select bill_account,total_due_amount from test.b where billing_date ='"""
+ billingdt + "'" + "and master_bill='" + sumbmaster + "'")
print("Statement:")
for row in cursor.fetchall():
a = row[0], "{:.2f}".format(row[1])
statementlist.append(a)
print(statementlist)
def main():
connect()
allocation(connect(),'2020-02-25','123')
statement(connect(), '2020-2-14', '345')
example
list 1 =[('123',1),('345', 2)]
list2 = [('123',1),('345', 2)]
output
('123',2), ('345',4)