Setting kwargs in a method using a loop over a list of strings

I would like to use a for loop to assign keyword variables. Instead of inserting all the keyword variables when calling the method ax.tick_params():

fig, ax = plt.subplots()

ax.tick_params(left="off", right="off", top="off", bottom="off")

Instead I would like to create a list and then loop over the list to assign the kwargs:

positions = ['left', 'right', 'top', 'bottom']

for p in position:
    ax.tick_params[p="off"]

This causes an error, I think because the elements in the list are strings and the kwargs are variable names. Is it possible to make this assignment work somehow?

This isn’t the reason why it fails (although that’s also wrong). It fails because that’s invalid syntax. That’s not how one uses functions.

Something that you can do is pass the parameter:argument pairs in a dictionary preceded by **. This will unpack the key:value pairs in the dictionary.

Here’s an example:

>>> def add(x,y):
...     return x+y
... 
>>> add(**{"x": 3, "y": -3})
0