I have a list of statements
a = "hello"
b = "hi"
c= "test"
In a program I got variable name(a,b or c) and need to return value of variable using that function method.
def my_method(item):
a = "hello"
b = "hi"
c= "test"
item parameter will be either a , b or c and I need to return value of that item. Is there any technique to return item value without if condition for three statement?
>Solution :
You can use a dictionary:
my_dict = { "a":"hello", "b":"hi", "c":"test" }
And then
def my_method(item):
return my_dict[item]
As @smci suggest, it can also be accessed directly without any function
my_dict[item]