Q:

Using dictionary to change the arguments of a variable

I'm trying to use a dictionary to change the value that is inserted in the arguments of a function.
horizon = 'YEARS'

Dict1 = {'YEARS':'weeks = 52*x','WEEKS':'weeks = x','DAYS':'days = x','HOURS': 'hours = x'} 

dummy = pd.to_datetime(pd.datetime.now())

dummy2 = [dummy - timedelta(weeks=x) for x in range(1,5)]

dummy3 = [dummy - timedelta((Dict1[horizon])) for x in range(1,5)]

In this case the dummy2 variable works but the dummy3 variable won't as Dict1[horizon][0] would give a string. 
Can I implement this in any other way?

A:

Yes you can, making use of dictionary unpacking and a lambda function. 
Dict1 = {'YEARS': lambda a: {'weeks': 52 * a}, ...

... timedelta(**Dict1[horizon](x)) ... 

How does this work?
Well, first Dict1['YEARS'] is returning a function: lambda a: {'weeks': 52 * a}. Which when called with for example 2 will return a one element dictionary: {'weeks': 104}. Then, this is unpacked by the double star (**) when passed to the function, turning timedelta(**{'weeks': 104}) into timedelta(weeks=104). Exactly as we wanted. 

