title: Python difference between filter() and map()
body: <p>Being new to python I am just trying to figure out the difference between filter() and map().
  I wrote a sample script as follows:</p>
  
  <pre><code>def f(x): return x % 2 == 0
  def m(y): return y * 2
  
  list = [1,2,3,4]
  
  flist = filter(f, list)
  print(list)
  print(flist)
  
  mlist = map(m, list)
  print(list)
  print(mlist)
  </code></pre>
  
  <p>We see that to both the filter and map we pass a list and assign their output to a new list.</p>
  
  <p>Output of this script is</p>
  
  <pre><code>[1, 2, 3, 4]
  [2, 4]
  [1, 2, 3, 4]
  [2, 4, 6, 8]
  </code></pre>
  
  <p>Question arises is that function call of both filter and map looks same so how will they behave if we interchange the contents of functions passed to them.</p>
  
  <pre><code>def f(x): return x * 2
  def m(y): return y % 2 == 0
  
  list = [1,2,3,4]
  
  flist = filter(f, list)
  print(list)
  print(flist)
  
  mlist = map(m, list)
  print(list)
  print(mlist)
  </code></pre>
  
  <p>This results in</p>
  
  <pre><code>[1, 2, 3, 4]
  [1, 2, 3, 4]
  [1, 2, 3, 4]
  [False, True, False, True]
  </code></pre>
  
  <p>This shows filter evaluates the function and if true it returns back the passed element.
  Here the function</p>
  
  <pre><code>def f(x): return x * 2
  </code></pre>
  
  <p>evaluates to</p>
  
  <pre><code>def f(x): return x * 2 != 0
  </code></pre>
  
  <p>In contrast map evaluates the function expression and returns back the result as items.
  So filter always expects its function to do comparison type of task to filter out the elements while map expects its functions to evaluate a statement to get some result.</p>
  
  <p>Is this understanding correct?</p>