title: Python remove entry from pickle saved data
body: <p>Firstly, I am aware of <a href="https://stackoverflow.com/questions/15114616/remove-or-edit-entry-saved-with-python-pickle">this post</a> which state that i have to <strong>rewrite the whole file</strong> in order for me to delete away 1 item from pickle saved data.</p>
  
  <p>I have a file which keeps the username and password(password's hash value) in binary form.It is created by this code:</p>
  
  <pre><code>import pickle
  import hashlib
  
  def Encryption(data):
      return hashlib.sha224(data).hexdigest()
  
  db = {'user1' : Encryption('password1'), 'user2' : Encryption('password2'), 'user3' : Encryption('password3')}
  fh = open('database.db', 'wb')
  pickle.dump(db, fh)
  fh.close()
  </code></pre>
  
  <p>I want to remove <code>user2,password2</code> (2nd entry) from the file. So  this is what i did</p>
  
  <pre><code>import pickle
  import hashlib
  from os import path
  
  n='user2'
  
  def Encryption(data):
      return hashlib.sha224(data).hexdigest()
  
  if path.isfile('database.db'):
      fh=open('database.db','rb')
      db=pickle.load(fh)
      fh.close()
  
  _newlist,_newlist2=([] for i in range (2))    
  _username=[]
  _password=[]
  
  #Get the user names and passwords hash values into two different list
  for user in db:
      _username.append(user)
      _password.append(db[user])    
  
  #If user name is equal to the user name i want to delete, skip . Else append it to new list
  for i in range(len(_username)):
      if n==_username[i]:
          pass
      else:
          _newlist.append(_username[i])
          _newlist2.append(_password[i])
  
  #Clear the file
  fh=open('database.db','wb')
  fh.close()
  
  #Re-write the new lists to the file
  for i in range(len(_newlist)):
      db={_newlist[i]:_newlist2[i]}
      fh = open('database.db', 'ab')
      pickle.dump(db,fh)
  </code></pre>
  
  <p>Instead of removing the 2nd entry(user2,password2), it removes all but last entry. Colud anyone help me point out what's wrong in my code?</p>