title: Append to list in a dictionary after setdefault
body: <p>I have the below code where I am trying to append a 1 to the hash of an element, on every occurence of it in input.</p>
  
  <pre><code>def test(Ar):
      hash_table = {}
      for elem in Ar:
          if elem not in hash_table:
              hash_table.setdefault(elem,[]).append(1)
          else:
              hash_table[elem] = hash_table[elem].append(1)
      print(hash_table)
  
  Ar = (1,2,3,4,5,1,2)
  test(Ar)
  </code></pre>
  
  <p>Output:</p>
  
  <pre><code>{1: None, 2: None, 3: [1], 4: [1], 5: [1]}
  </code></pre>
  
  <p>Expected Output:</p>
  
  <pre><code>{1: [1,1], 2: [1,1], 3: [1], 4: [1], 5: [1]}
  </code></pre>
  
  <p>I am puzzled why None gets in on doing an append. Kindly explain what is happening.</p>
  
  <p>Note:</p>
  
  <p>On typing the else portion,</p>
  
  <pre><code>hash_table[elem] = hash_table[elem].append(1) # the append() was not suggested at all by the IDE. I forcibly put it, hoping things will work.
  </code></pre>