1
inventory = {'A':['Toy',3, 1000], 'B':['Toy',8, 1100], 
              'C':['Cloth',15, 1200], 'D':['Cloth',9, 1300], 
               'E':['Toy',11, 1400], 'F':['Cloth', 18, 1500], 'G':['Appliance', 300, 50]}

The alphabets are name of merchandise, the first field in the [] brackets are category of the merchandise, the second field in the [] brackets are price, the third are numbers sold.

I would like the price to be increased by 1 so the result would look like.

inventory = {'A':['Toy',4, 1000], 'B':['Toy',9, 1100], 
              'C':['Cloth',16, 1200], 'D':['Cloth',10, 1300], 
               'E':['Toy',12, 1400], 'F':['Cloth', 19, 1500], 'G':['Appliance', 301, 50]} 

Then what would be a good way to loop through to find any item whose price is $19.

I am not good at lambda function. Could you please give me some explanation of your code so that I could manipulate for future use? Thank you

Ujae Kang
  • 157
  • 1
  • 8
  • Finding items whose price is $19 is not as efficient. Lookups for key in `dict` is very fast (`O(log N)` or even `O(1)`, but searching for a value you need to read through all elements (`O(N)`). – skyking Aug 10 '15 at 08:28
  • You are asking two questions. Try to ask a single question! – Sascha Gottfried Aug 10 '15 at 08:38
  • Not really an answer to the question, but it sounds like you don't really want a list for each key. If you have a fixed set of things that each object has it's easier and less error prone to create a class for it and access things by name rather than just using list indices (eg `x.category`, `x.price`, `x.number_sold`). – Holloway Aug 10 '15 at 09:29

3 Answers3

2

Try this:

for k, v in inventory.iteritems():
    v[1] += 1

Then to find matches:

price_match = {k:v for (k,v) in inventory.iteritems() if v[1] == 19}

A lambda to find matches:

find_price = lambda x: {k:v for (k,v) in inventory.iteritems() if v[1] == x}
price_match = find_price(19)
M. Shaw
  • 1,742
  • 11
  • 15
  • 1
    Note that in that example the `lambda` looks like an excuse for using `lambda`. You could use an ordinary function instead. – skyking Aug 10 '15 at 08:34
  • @skyking OP said `I am not good at lambda function.`. I'm assuming he wants a lambda function of some sort (?). But anyways it is trivial to change it into an ordinary function. – M. Shaw Aug 10 '15 at 08:41
0

You can use a dict comprehension :

>>> new={k:[m,p+1,n] for k,(m,p,n) in inventory.items()}
{'G': ['Appliance', 301, 50], 'E': ['Toy', 12, 1400], 'D': ['Cloth', 10, 1300], 'B': ['Toy', 9, 1100], 'A': ['Toy', 4, 1000], 'C': ['Cloth', 16, 1200], 'F': ['Cloth', 19, 1500]}
>>> 

And for finding a special items :

>>> {k:[m,p,n] for k,(m,p,n) in new.items() if p==19}
{'F': ['Cloth', 19, 1500]}
Community
  • 1
  • 1
Mazdak
  • 105,000
  • 18
  • 159
  • 188
0

If you wan't to modify the data "in-place" you would loop through the dict:

for k, v in inventory.iteritems():
    v[1] += 1

if you're using python3 you'd use items instead of iteritems. The solution with using dict comprehension {k:[m,p+1,n] for k,(m,p,n) in inventory.items()} means that you will replace the entire dict with a new obejct (which is fine in some scenarios, but not that good in others).

skyking
  • 13,817
  • 1
  • 35
  • 57