Python how to subtract 2 lists ( product and sales ) like a beginner?

Question:

inventory = {'Dress': 200, 'Pants': 100, 'Shorts': 250, 'Tops': 250, 'Coats_and_Jacket': 150, 'Shoes': 250, 'Accessories': 150}
sales = {'Accessories': 25, 'Shorts': 100, 'Dress': 75, 'Pants': 50, 'Tops': 175, 'Coats_and_Jacket': 120 }
max_inventory = {}
max_sales = {}
result = {}
print("Items with maximum inventory are the following:")
for name,stock in inventory.items():
  if stock == max(inventory.values()):
    max_inventory[name] = stock
print(max_inventory)
print()
print("Items with maximum sale are the following:")
for name,sale in sales.items():
  if sale == max(sales.values()):
    max_sales[name] = sale
print(max_sales)
print("Current inventory is the following:")
for name,value in inventory.items():
  if name in sales.keys():
    result[name] = inventory.values() - sales.values()
result

I have searched about this and none of them is the one I have learn in class ( yet ) so Is there an easy way to subtract 2 values of list?
and also how do I do it in Pythonic way (using only list comprehension )?

Asked By: Nooboolean

||

Answers:

You could try it for the subtraction of dictionaries:

results = {key: inventory[key] - sales.get(key, 0) for key in sales.keys()}
print("The difference of dictionaries is : " + str(results))

And for lists, try this way(l1 and l2 are your lists):

for i1 , i2 in zip(l1,l2):
    sub.append(i1-i2)
Answered By: RezaShojaeivand

You’d need a dictionary comprehension (see Python Dictionary Comprehension), instead of just list comprehension. And you need to use sales.get(key, default) to allow for cases where a key exists in your inventory, but not sales dictionary:

inventory = {'Dress': 200, 'Pants': 100, 'Shorts': 250, 'Tops': 250, 'Coats_and_Jacket': 150, 'Shoes': 250, 'Accessories': 150}
sales = {'Accessories': 25, 'Shorts': 100, 'Dress': 75, 'Pants': 50, 'Tops': 175, 'Coats_and_Jacket': 120 }

remaining_inventory = {item:(inventory[item] - sales.get(item, 0)) for item in inventory}

print(remaining_inventory)

Output:

{'Dress': 125, 'Pants': 50, 'Shorts': 150, 'Tops': 75, 'Coats_and_Jacket': 30, 'Shoes': 250, 'Accessories': 125}
Answered By: PangolinPaws

Are you trying to calculate your updated inventory dictionary based on your sales dictionary? (Note that these are key: value dictionaries not lists)

You could do something like:

from typing import Dict


def update_inventory(current_inventory: Dict[str, int],
                     sales: Dict[str, int]) ->  Dict[str, int]:
    new_inventory = {}
    for name in current_inventory.keys():
        if name in sales:
            new_inventory[name] = current_inventory[name] - sales[name]
    return new_inventory


my_inventory = {'Dress': 200, 'Pants': 100, 'Shorts': 250, 'Tops': 250, 'Coats_and_Jacket': 150, 'Shoes': 250, 'Accessories': 150}
my_sales = {'Accessories': 25, 'Shorts': 100, 'Dress': 75, 'Pants': 50, 'Tops': 175, 'Coats_and_Jacket': 120 }

my_inventory = update_inventory(my_inventory, my_sales)

I like having a function like this because it is readable and maintainable. A "pythonic" way of doing the dictionarysubtraction would be something like

upated_inventory = {k: my_inventory[k] - my_sales.get(k, 0) for k in my_inventory.keys()}
Answered By: tomcheney

Subtraction of dictionaries:

result = {name : inventory[name] - sales[name] for name in inventory.keys() if name in sales.keys()}

List comprehension style of your code

inventory = {'Dress': 200, 'Pants': 100, 'Shorts': 250, 'Tops': 250, 'Coats_and_Jacket': 150, 'Shoes': 250, 'Accessories': 150}
sales = {'Accessories': 25, 'Shorts': 100, 'Dress': 75, 'Pants': 50, 'Tops': 175, 'Coats_and_Jacket': 120 }
max_inventory = {}
max_sales = {}
result = {}
print("Items with maximum inventory are the following:")

max_inventory ={name: stock for name,stock in inventory.items() if stock == max(inventory.values())}

print(max_inventory)
print()
print("Items with maximum sale are the following:")

max_sales = {name: sale for name,sale in sales.items() if sale == max(sales.values())}

print(max_sales)
print("Current inventory is the following:")

result = {name : inventory[name] - sales[name] for name in inventory.keys() if name in sales.keys()}

result
Answered By: Shahab Rahnama
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.