Skip to content
Advertisement

Python – Iterating through 2 list with arithmetic function

I have 2 python list, stock_level to track the amount of inventory and required to indicate the amount of each item in stock_level needed to create each product.

stock_level = [70, 20, 20 , 20] 
required = [20, 20, 20, 20]

How do I iterate through the 2 lists to find out how many products can be made and the inventory left outstanding after making the products i.e. given the above value, only 1 product can be made and stock_level should end up to be [50, 0, 0, 0]

EDIT: The products can only be made if all the values in stock_level is greater than required. If,

stock_level = [20, 20, 20, 20]
required = [70, 20, 20, 20]

No products should have been made as the first element in stock_level is lesser than the first element in required. A product should only be made if the value in stock_level is greater than the corresponding element in required.

stock_level = [70, 20, 20, 20]
required = [20, 20, 20, 20]

In this example, only 1 product should be made as after the first run, stock_level will become [50, 0, 0, 0] and there won’t be enough inventory in the 2nd to 4th element of stock_level to make another product.

I came up with something like this.

made = 0
for x in required:
    for y in stock_level:
        while True:
            if x > y:
                print("Insufficient")
                break
            else:
                # stock_leve minus required
                print("Product created")
            made += 1

Advertisement

Answer

Your code is nesting the loop but you need to go through both list in parallel. Also you end up with an infinite loop.

While zipping the array is the best option, find here an adjusted version using enumerate, it should help you understand.

Not managed the case where there is insufficient stock.

stock_level = [70, 20, 20 , 20] 
required = [20, 20, 20, 20]

for i, x in enumerate(required):
    y = stock_level[i]
    if x > y:
        print("Insufficient")
    else:
        # stock_leve minus required
        print("Product created")
        made = y - x
        stock_level[i] = made

stock_level #=> [50, 0, 0, 0]

Updating after the edit, if I got the point. The problem is still the nested loop and the logic itself used.

To understand the following implementation, please, dig into the doc:

Here is a possible implementation:

stock_level = [70, 40, 40, 50] 
required = [20, 20, 20, 20]

tmp = zip(stock_level, required)
tmp = list(zip(stock_level, required))
makeable = 0

while True:
    update_make = tmp
    tmp = [ [stk - req, req] for stk, req in tmp ]
    can_be_made = all([stk >= 0 for stk, _ in tmp])
    if can_be_made:
        makeable += 1
    else:
        break
        
print(makeable) #=> 2
print(update_make) #=> [[30, 20], [0, 20], [0, 20], [10, 20]]
actual_stock_level = [stk for stk, req in update_make]
print(actual_stock_level) #=> [30, 0, 0, 10]
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement