List / list – how to find the next element

An example of a python function iterating over a list in which we access the next element.

Using the function zip you can get the previous object. However, we will go with a simple enumeration.

Function principle:
— First we sort the list in the reverse direction.
– At the first iteration, we assign the value
– At the second iteration, we will be able to work with this value
– At the end, sort again

def calculate_prev_diff_balance(data):
data.reverse()

prev = None
for x in data:
if prev_sum:
x[‘new_sum’] = x[‘sum’] -prev_sum
prev_sum = x[‘sum’]

data.reverse()
return data

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *