title: How to call a function with a dictionary that contains more items than the function has parameters?
body: <p>I am looking for the best way to combine a function with a dictionary <em>that contains more items than the function's inputs</em></p>
  
  <p>basic **kwarg unpacking fails in this case:</p>
  
  <pre><code>def foo(a,b):
      return a + b
  
  d = {'a':1,
       'b':2,
       'c':3}
  
  foo(**d)
  --&gt; TypeError: foo() got an unexpected keyword argument 'c'
  </code></pre>
  
  <p>After some research I came up with the following approach:</p>
  
  <pre><code>import inspect
  
  # utilities
  def get_input_names(function):
      '''get arguments names from function'''
      return inspect.getargspec(function)[0]
  
  def filter_dict(dict_,keys):
      return {k:dict_[k] for k in keys}
  
  def combine(function,dict_):
      '''combine a function with a dictionary that may contain more items than the function's inputs '''
      filtered_dict = filter_dict(dict_,get_input_names(function))
      return function(**filtered_dict)
  
  # examples
  def foo(a,b):
      return a + b
  
  d = {'a':1,
       'b':2,
       'c':3}
  
  print combine(foo,d)
  --&gt; 3
  </code></pre>
  
  <p>My question is: is this a good way of dealing with this problem, or is there a better practice or is there a mechanism in the language that I'm missing perhaps?</p>