title: python dictionary.get vs in dictionary
body: <p>Considering <a href="https://leetcode.com/problems/number-of-dice-rolls-with-target-sum/" rel="nofollow noreferrer">leetcode 1155</a></p>
  <p>This piece of code exceeds the time limit.</p>
  <pre><code>class Solution:
      def numRollsToTarget(self, dices: int, faces: int, target: int) -&gt; int:
          
          dp = {}
          def ways(t, rd):
              if t == 0  and rd == 0: return 1
              if t &lt;= 0 or rd &lt;= 0: return 0
              if  dp.get((t,rd)): 
                  return dp[(t,rd)]
              dp[(t,rd)] = sum(ways(t-i, rd-1) for i in range(1,faces+1))
              return dp[(t,rd)]
          
          return ways(target, dices) % (10**9 + 7)
  </code></pre>
  <p>While this does not:</p>
  <pre><code>class Solution:
      def numRollsToTarget(self, dices: int, faces: int, target: int) -&gt; int:
          
          dp = {}
          def ways(t, rd):
              if t == 0  and rd == 0: return 1
              if t &lt;= 0 or rd &lt;= 0: return 0
              if  (t,rd) in dp: 
                  return dp[(t,rd)]
              dp[(t,rd)] = sum(ways(t-i, rd-1) for i in range(1,faces+1))
              return dp[(t,rd)]
          
          return ways(target, dices) % (10**9 + 7)
              
  </code></pre>
  <p>Only difference is <code>dp.get(tuple)</code> vs <code>tuple in dp</code>. AFAIK, both operations are O(1) in terms of time complexity. What is causing the difference here.</p>
  <p>One reason i can think of is that in case of dp.get() call, if there is lot of hash conflicts, it might have to internally traverse the linked list to get the <code>None</code>. But i am not sure how <code>in</code> call will avoid that linked list traversal either.</p>
  <p>Any way to debug?</p>
  <p>[Edit]</p>
  <p>To confirm, tuple is not causing any problem, I converted tuple to &quot;-&quot; separated string also. Still the same behaviour.</p>