title: Why do I get duplicate keys in dictionaries and in lists in Python?
body: <p>My problem is, I sometimes get duplicate keys in dictionaries or lists (checking if the item is there beforehand). I couldn't manage to reproduce it with any consistency.</p>
  <p>Currently I am saving a list of users accessing my telegram bot in some way like:</p>
  <pre><code>async def myfunction()
   ..........
       users={}
       if os.access(&quot;userlist.json&quot;, os.R_OK):
           with open ('userlist.json') as f:
               users=json.load(f)
       currentuser=[str(update.message.from_user.id),update.message.from_user.username,update.message.from_user.first_name,update.message.from_user.last_name,False,1]
       if currentuser[0] in users:
           currentuser[5]+=users[currentuser[0]][5] #counting how many messages the user sent to the bot
       users[currentuser[0]]=currentuser
       with open ('userlist.json','w') as f:
           json.dump(users,f)
  ........
  </code></pre>
  <p>Here's in example of a json dump of a dictionary from the server:</p>
  <pre><code>{&quot;1033556742&quot;: [1033556742, &quot;CatchingStars&quot;, &quot;Starcatcher&quot;, null, false, 1], &quot;1033556742&quot;: [1033556742, &quot;CatchingStars&quot;, &quot;Starcatcher&quot;, null, false, 1]}
  </code></pre>
  <p>Also I have a similar patch of code where I add items to a list in a similar way: I load a list from json, I check if the item is in the list with:</p>
  <pre><code>if item in list: print ('ok')
  else: 
       list.append(item)
       with ('mylist.json','w') as f:
             json.dump(list,f)
  </code></pre>
  <p>and I get duplicate items there too, sporadically.</p>