Modifying python nested dictionary based on values elsewhere in the dictionary

Apologies if this is the incorrect place to post this but I’ve been at it a while and have been unable to come up with anything. I have one dictionary as follows in the format of {person1: {person2: amount, person3: amount}} where person1 is owed money by person2 and person3 to the value of their amount.

owings (dictionary name) prior to code

{‘Josh’: {‘Ayami’: 1131.432305272121, ‘Pete’: 1000.0, ‘Sven’: 1014.4323052721211, ‘Ashtyn’: 1000.0}, ‘Pete’: {‘Ayami’: 125.0, ‘Josh’: 125.0, ‘Sven’: 125.0, ‘Ashtyn’: 0.0}, ‘Ayami’: {‘Josh’: 1725.0, ‘Pete’: 0.0, ‘Sven’: 0.0, ‘Ashtyn’: 0.0}}

I need to adjust the values in the dictionary so that if people owe each other money then the amounts will be subtracted so that one persons value is 0 (ideally they would be removed from the dictionary but I’ll tackle that later) and the other persons is their amount less the amount that was removed from the first person so that only one person owes the other money.

Currently my code is this but I’ve been unable to figure out where it’s going wrong, appreciate any hints.

for m2p, v in owings.items():
        for mfp, amount in v.items():
            if (mfp in owings.keys()) and (m2p in owings[mfp].keys()) and (amount > owings[mfp][m2p]):
                amount = amount - owings[mfp][m2p]
                owings[mfp][m2p] = 0
            elif (mfp in owings.keys()) and (m2p in owings[mfp].keys()) and (amount < owings[mfp][m2p]):
                owings[mfp][m2p] = owings[mfp][m2p] - amount 
                amount = 0
            elif (mfp in owings.keys()) and (m2p in owings[mfp].keys()) and (amount == owings[mfp][m2p]):
                owings[mfp][m2p] = 0 
                amount = 0

owings after code.

{‘Josh’: {‘Ayami’: 537.8646105442422, ‘Pete’: 1000.0, ‘Sven’: 1014.4323052721211, ‘Ashtyn’: 1000.0}, ‘Pete’: {‘Ayami’: 125.0, ‘Josh’: 0, ‘Sven’: 125.0, ‘Ashtyn’: 0.0}, ‘Ayami’: {‘Josh’: 593.5676947278789, ‘Pete’: 0, ‘Sven’: 0.0, ‘Ashtyn’: 0.0}}

You can cut down on your code significantly if exchanged some of your key checks (chained if/and statements might benefit from the all() function), (mfp in owings.keys()) to a simpler mfp in owings and lines like amount = amount - owings[mfp][m2p]
to amount -= owings[mfp][m2p].

On that note, what happens in this first if statement. What do you want to happen? Does the code reflect your logic?

Yeah I had some of that in there before but expanded it out when I got stuck as I thought it would make it look more readable. Not sure if I succeeded though :stuck_out_tongue:

So the if/elif statements are meant to change the values in the dictionary to simplify the owings between the people. For example.

{Josh: {Ayami: 100}, Ayami: {Josh: 50}} should change so that it becomes {Josh: {Ayami: 50}, Ayami: {Josh: 0}}

This should be done across all people in the nested dictionary so that everyone would know what amount of money they owe everyone.

Haha. For me at least, definitely not. I was querying the if statement in particular as that’s as far as I read before catching an error because it only does half of what you think it’s doing. What are you doing with amount?

Yeah my bad XD
So the amount variable should be set to the value of the person owing money. So the structure should be this: {Person_receiving_money: {Person_sending_money_1: amount_they_owe, Person_sending_money_2: amount_they_owe}}. So amount should be the value being updated based on if the person sending money and the person receiving money both owe each other an amount of money. It doesn’t really make sense for one person to give another $50 and then that same person to give $100 back to them right? So I want the dictionary to be updated so that one value should go to $0 and the other should be $50 in this instance representing one exchange of money instead of two between the same people. Does that make sense? If there’s a better way to do this I’m open to it haha.
The multiple if/elif statements were there to attempt to catch each possible scenario for how much money each of the two people in question owe.

Sorry for going about this in a roundabout way but I’m hoping to get you spot the issue yourself. Try running your code up to the execution of the first if statement (for the given dictionary the if statement is met on the first ieration and we can ignore elif) and then check the values of your dictionary. Have they been updated in the way you expected? Something like breaking out of any and all loops and printing owings would do the trick.