0

I have a function called filter that accepts a number of parameters in the format name=value. I have 2 lists: names and values (let's call them x and y), I need to be able to pass these into the function.

When I just pass in the string as in the below example, it gives me an error:

x = ['t', 't2']
y = [w, w2]
filter(x[0]=y[0], x[1]=y[1])

SyntaxError: keyword can't be an expression

I would also need to be able to pass in an arbitrary amount, for the size of the lists. How can I go about making this work?

Supez38
  • 329
  • 1
  • 3
  • 16

2 Answers2

3

Use **kwargs:

kwargs = {x[0]: y[0], x[1]: y[1]}
filter(**kwargs)
iBug
  • 35,554
  • 7
  • 89
  • 134
  • Thanks so much, this was exactly it lol. New to kwargs, I thought I could pass in a dictionary but it wasn't really working before. – Supez38 Dec 21 '18 at 16:53
0

You probably don't want to have a function called filter as that will overwrite the built-in python filter function. But otherwise read up on using *args and **kwargs, sounds like what you need.

bjk116
  • 531
  • 3
  • 16