title: Creating a Python list from a map using dictionary get() as a function vs creating a dictionary get() list with a for loop
body: <p>I was very surprised that a seemingly easier and less taxing function was less efficient.</p>
  <p><strong>I ran a test on two methods, with a 10000 a-j (randomized) characters long string as <code>word</code>.</strong> The old function is <code>a</code>, the new one is <code>b</code>:</p>
  <pre><code>def a(word):
  dictionary = {&quot;a&quot;: 1, &quot;b&quot;: 2, &quot;c&quot;: 3, &quot;d&quot;: 4, &quot;e&quot;: 5, &quot;f&quot;: 6, &quot;g&quot;: 7, &quot;h&quot;: 8, &quot;i&quot;: 9, &quot;j&quot;: 10,}
  word = list(map(dictionary.get, list(word)))
  return word
  
  def b(word):
  dictionary = {&quot;a&quot;: 1, &quot;b&quot;: 2, &quot;c&quot;: 3, &quot;d&quot;: 4, &quot;e&quot;: 5, &quot;f&quot;: 6, &quot;g&quot;: 7, &quot;h&quot;: 8, &quot;i&quot;: 9, &quot;j&quot;: 10,}
  word = [dictionary.get(l, l) for l in word]
  return word
  </code></pre>
  <p><code>map</code> is supposed to have a tiny speed advantage vs a for loop: <a href="https://stackoverflow.com/a/1247490/12494235">https://stackoverflow.com/a/1247490/12494235</a>. However, in my case, the advantage is significant. Many more operations are being executed in function <code>b</code>, and it consistently takes 0.001-0.003 seconds longer. Also, converting the map iterable into a list should slow down <code>a</code>.</p>
  <p><em>A - creating a list from a map using dictionary get() as a function</em></p>
  <pre><code>         160 function calls (159 primitive calls) in 0.002 seconds
  
     Ordered by: call count
     ncalls  tottime  percall  cumtime  percall filename:lineno(function)
         12    0.000    0.000    0.000    0.000 {method 'rstrip' of 'str' objects}
          7    0.000    0.000    0.000    0.000 {method 'join' of 'str' objects}
          6    0.000    0.000    0.000    0.000 &lt;frozen importlib._bootstrap&gt;:222(_verbose_message)
          6    0.000    0.000    0.000    0.000 {built-in method builtins.getattr}
          ...
  </code></pre>
  <p><em>B - creating a dictionary get() list with a for loop</em></p>
  <pre><code>         10161 function calls (10160 primitive calls) in 0.004 seconds
  
     Ordered by: call count
     ncalls  tottime  percall  cumtime  percall filename:lineno(function)
      10002    0.001    0.000    0.001    0.000 {method 'get' of 'dict' objects}
         12    0.000    0.000    0.000    0.000 {method 'rstrip' of 'str' objects}
          7    0.000    0.000    0.000    0.000 {method 'join' of 'str' objects}
          6    0.000    0.000    0.000    0.000 &lt;frozen importlib._bootstrap&gt;:222(_verbose_message)
          6    0.000    0.000    0.000    0.000 {built-in method builtins.getattr}
          ...
  </code></pre>
  <p><strong>How is that even possible? Did I do something wrong when profiling the two? Why is a for loop so inefficient?</strong></p>