title: Fastest way to iterate over a Python3 map
body: <p>I recently asked <a href="https://stackoverflow.com/questions/50028939/is-there-a-stack-and-map-analogue-with-dicts/50030625#50030625">this</a> question, and I modified one of the solutions, which uses a loop, to use a map instead. Of course, in Python3, <code>map()</code> returns an iterator and will not execute the callback until it is iterated over. Consider the following dictionary and mapping functions:</p>
  
  <pre><code>files = {'Code.py': 'Stan', 'Output.txt': 'Randy', 'Input.txt': 'Randy'}    
  </code></pre>
  
  <p>is the dictionary </p>
  
  <pre><code>mapped = {}
  
  for k, v in files.items():
      mapped.setdefault(v, []).append(k)
  </code></pre>
  
  <p>Runs almost twice as fast as:</p>
  
  <pre><code>mapped = {}
  
  any(map(lambda i: mapped.setdefault(i[1],[]).append(i[0]),files.items()))
  </code></pre>
  
  <p>My (uneducated) guess is that building the map object and then iterating over it is just less efficient than performing the callback in a real loop. My question is: Is there a better way to use <code>map</code> that either executes the callback on build, or is there a faster way to iterate over the map than using <code>any</code>? For reference, here are the runtimes of each:</p>
  
  <pre><code>#timeit
  #using setdefault with any, lambda, map:
  #1.46 µs ± 12.2 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
  #using setdefault with loop:
  #826 ns ± 2.75 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
  </code></pre>