1
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 )?

Nooboolean
  • 47
  • 6

4 Answers4

3

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}
PangolinPaws
  • 670
  • 4
  • 10
  • May I ask about `{item:(inventory[item] - sales.get(item, 0)) for item in inventory}` this? In my class I only see `for item in inventory` but what's with the `{item:(inventory[item] - sales.get(item, 0))` infront of the `for` – Nooboolean Sep 11 '22 at 14:46
  • From what I know is that it is an expression but I'm still don't know how it worked – Nooboolean Sep 11 '22 at 14:56
  • 2
    @Nooboolean that notation is dictionary comprehension https://peps.python.org/pep-0274/ Also checkout this question: https://stackoverflow.com/questions/14507591/python-dictionary-comprehension – tomcheney Sep 11 '22 at 15:00
  • 1
    tomcheney is right, that notation makes sure you get a dictionary like your two original ones as the result @Nooboolean. I've added tomcheney's link to the answer for completeness – PangolinPaws Sep 11 '22 at 15:11
  • @PangolinPaws May I ask a little extra question? What If 'Dress':200 changed to 'Dress':(200,10) how do I access 10? – Nooboolean Sep 11 '22 at 15:25
  • 2
    @Nooboolean The value `(200, 10)` is a tuple, so you can access the individual elements with an indes: `inventory]'Dress'][1]` – Matthias Sep 11 '22 at 20:44
0

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)
0

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()}
tomcheney
  • 1,750
  • 1
  • 17
  • 33
0

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
Shahab Rahnama
  • 982
  • 1
  • 7
  • 14
  • 1
    what does `value` represent in your code? How could you use it? – cards Sep 11 '22 at 14:56
  • @cards you are right there is no need for that :) – Shahab Rahnama Sep 11 '22 at 14:59
  • also possible `{name : value - sales[name] for name, value in inventory.items() ...}` – cards Sep 11 '22 at 15:01
  • @cards which is better, searching in a `list` or in a `dict`? – Shahab Rahnama Sep 11 '22 at 15:03
  • I would say that a double look-up is more expensive, so the `item` approach should be better, one should `timeit` PS a dictionary iterate by default along its keys so no need for `.keys()` – cards Sep 11 '22 at 15:07
  • @cards looking up in a dictionary by key is O(1) and `iterating a list is definitely faster.` look at this: https://stackoverflow.com/questions/12543837/python-iterating-over-list-vs-over-dict-items-efficiency – Shahab Rahnama Sep 11 '22 at 15:11
  • but it is not an iteration contest, there are also operation which take place at each iteration. A [workbench](https://onlinegdb.com/3bUsXH2hi) – cards Sep 11 '22 at 15:25