0

could you please help me with this.

I want to print the value of the key with if statement. it is have one OR other value if it is not , but not for one , for all.

my try:

wp = {'tomatos': , 'patotoes': , 'milk':0.5'cheese':, 'eggs':0.25,'meat':2}
x= ' item is not available in this store'

how I can make my output like this ?

tomatos item is not available in this store.

patotoes item is not available in this store.

milk 0.5

cheese item is not available in this store .

eggs 0.25

meat 2

thats mean if any Item in the list dont have price , print x infront of it , and for others print the price shown .

Alexander Gessler
  • 45,603
  • 7
  • 82
  • 122
shz
  • 1
  • 1
  • 2

2 Answers2

2

There are tons of ways to do what you are asking but the below would work:

wp = { 'tomatos': None,
       'patotoes': None ,
       'milk':0.5,
       'cheese': None, 
       'eggs':0.25,
       'meat':2}
x= ' item is not available in this store'

for k,v in wp.items():
   print "%s%s" % (k, (v if v is not None else x))

Please not the changes to wp.

Nix
  • 57,072
  • 29
  • 149
  • 198
  • Can I do it without putting any value ? – shz Apr 28 '11 at 00:49
  • I would suggest "is None" rather than "!=None" @shz: You must supply some value for every key that you put into a dictionary. "None" is the Python object which is conventionally used as a sentinel or placeholder for such things. Presumably you were getting syntax errors from the attempts to execute that `wp=` statement in your example. – Jim Dennis Apr 28 '11 at 01:03
  • @Jim Dennis Any reason you suggest is None vs !=None? jw. – Nix Apr 28 '11 at 01:05
  • @shz I do not know of any way to do it with out a value. – Nix Apr 28 '11 at 01:08
  • Why you should use `v is not None` explained here: [Why is if not someobj: better than if someobj == None: in Python?](http://stackoverflow.com/questions/100732/why-is-if-not-someobj-better-than-if-someobj-none-in-python). – samplebias Apr 28 '11 at 01:09
  • I originally had is not None, but figured it would be easier to read with != . (Reading your article now) – Nix Apr 28 '11 at 01:13
  • @Nix thank you very much.if I want to put value for each one , it will take too much time if I have a hug list of food name (for example), should be some way to fix it. but again thank you – shz Apr 28 '11 at 01:15
  • How are you generating the dictionary? By hand, I am sure there is a way to automate it. – Nix Apr 28 '11 at 01:16
  • @Nix you can often get away is != ... but when using None you almost always want to test identity rather than equality. Incidentally if you want to create a sentinel object other than None you can use: mysentinel = object() and then test against that specific instance's identity. (This would be for some hypothetical case where you need a sentinel but you might need to pass references to None on to some other API that might accept them. Any object created by object() will have a unique identity, distinct from all other objects. – Jim Dennis Apr 28 '11 at 04:27
1

Use the fact that the 'get' method of a dictionary returns None (by default) for a key that's not in the dictionary.

itemPrices = { 'milk' : 0.5, 'eggs' : 0.25, 'meat' : 2.0 }
sorry = 'Sorry, %s is not available in this store.'

for itemName in ('milk', 'potatos', 'eggs'):
    price = itemPrices.get(itemName)
    if price is None:
        print sorry % itemName
    else:
        print itemName, price
Michael Kent
  • 1,736
  • 12
  • 11
  • +1 good suggestion. `print itemName, itemPrices.get(itemName, "item is not available in this store.")`. This doesn't provide your nice string formatting, but demonstrates providing your own default to get. – kevpie Apr 28 '11 at 05:28