3

I am trying to convert an "if else" statement in python into a dictionary.

I tried to convert it into a dictionary, but how do I handle the last else clause?

val=3

if val==1:
  print "a"
elif val==2:
  print "b"
elif val==3:
  print "c"
elif val==4:
  print "d"
else:
  print "value not found:"

print "===========Converted if else into dictionary ==================="

DATA_SOURCE = {1:"a",2:"b",3:"c",4:"d"}
print DATA_SOURCE[val]

I have created this code as a replacement:

if not DATA_SOURCE.has_key(val):
  print "value not found:"
else:
  print DATA_SOURCE[val]

Is it equivalent?

Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
  • Why exactly are you trying to eliminate if/else? – x otikoruk x Sep 03 '15 at 05:53
  • 1
    @Thecheater887 may be to reduce the number of coding lines – The6thSense Sep 03 '15 at 05:54
  • @Thecheater887 See [Why is the 'if' statement considered evil?](http://stackoverflow.com/questions/1554180/why-is-the-if-statement-considered-evil) – Peter Wood Sep 03 '15 at 06:07
  • Your `has_key` condition can be written as `val not in DATA_SOURCE`. I prefer this way. – Moberg Sep 03 '15 at 06:08
  • Wow. Go here and get attacked. That was great! I forgot why I left SO, but now I think I rememeber.. – x otikoruk x Sep 03 '15 at 06:09
  • @Thecheater887 it is a place to learn mate not a place to get attacked and happy learning – The6thSense Sep 03 '15 at 06:14
  • 1
    @Thecheater887 I thought you'd find the question and answers interesting. Really didn't mean it as an attack )c: – Peter Wood Sep 03 '15 at 06:18
  • @VigneshKalai The struggle of being looked down upon because of my rep, and answering bluntly, but correctly rather than a general 'figure it out by yourself and come back later' is real much. Very few people are like you that they give a response to a question without bringing their flamethrowers, and I appreciate that. --Can I give you a cookie? – x otikoruk x Sep 03 '15 at 06:20
  • 1
    @PeterWood SO is a place of learning, and I just learned being blunt with a blunt person hurts a lot more than I remembered. Bridges are mended. ((Now back on topic..)) – x otikoruk x Sep 03 '15 at 06:22
  • @Thecheater887 yeah a cookie would be nice :P and I too have been in the same position reputation does not matter learn from what is thrown at you and Peter wood did throw a valuable piece of gold – The6thSense Sep 03 '15 at 06:25
  • 2
    @VigneshKalai That depends on the eyes of the person that sees it. Someone might see it as gold, while others, like I, see it as an attack.. I guess it's time that I should upgrade the `perspective` module to v0.0.2 :P – x otikoruk x Sep 03 '15 at 06:27
  • @Thecheater887 verion v0.0.2 it is .The question which you asked was very elementary to some people and what peter wood gave was a very valuable information even I did not know that :P. Just enjoy learning and move forward – The6thSense Sep 03 '15 at 06:33
  • 1
    @Thecheater887 I hope you stick around. I didn't expect to be seen as blunt. Articles about removing conditionals from code have really helped my thinking about design. I just responded to your question with a relevant SO link. See [Unconditional Programming](http://michaelfeathers.typepad.com/michael_feathers_blog/2013/11/unconditional-programming.html), and [When A Method Can Do Nothing](https://michaelfeathers.silvrback.com/when-it-s-okay-for-a-method-to-do-nothing) from the great [Michael Feathers](http://www.amazon.co.uk/Working-Effectively-Legacy-Michael-Feathers/dp/0131177052). Peace. – Peter Wood Sep 03 '15 at 07:55
  • Not that I personally care too much which answer you selected, but why did you pick the more complicated, less pythonic one? This might be confusing to future readers of this post. – juanchopanza Sep 04 '15 at 13:22

2 Answers2

9

You can use the dict.get method:

print DATA_SOURCE.get(val, "value not found")

That will return "value not found" if val is not a key, without affecting the dictionary.

As always, if in doubt, use the help:

>>> help(dict)
juanchopanza
  • 223,364
  • 34
  • 402
  • 480
0

I think you are looking for;

val = 3
dct = {1:"a", 2:"b", 3:"c", 4:"d"}
if dct.get(val) == None: print "Value Not Found"
x otikoruk x
  • 422
  • 1
  • 6
  • 16