I am trying to implement a function by using the following example from Replacements for switch statement in Python:
def f(x):
return {
'+': (lambda x: x[1] + x[2]),
'-': (lambda x: x[1] - x[2])
}[x[0]]
print(f(["+",2,3])) #Output should be 5.
f(["-", 4, 1]) # Output should be 3.
I am clearly doing something wrong. Maybe someone has even a better suggestion for this?
EDIT:
What if I wanted to save the output of the lambda in a list and then return it?
For example:
def f(x, output):
return {
'+': (lambda x: x[1] + x[2]),
'-': (lambda x: x[1] - x[2])
}[x[0]]
output = [10, 20, 30, 40]
# to 2nd element in output list we add the last element in the list
print(f(["+",2,3], output)) #Output should be output = [10, 20, 33, 40]
output = [10, 20, 30, 40]
f(["-", 1, 100]) # Output should be [10, 120, 30, 40].