title: Python - Create million of unique alphanumeric codes with high performance
body: <p>actually I am stuck with the following business case and do not have an idea how to solve it.</p>
  
  <p>I have to create more than 5.000.000 unique alphanumeric codes.</p>
  
  <p>The rules for the codes are:</p>
  
  <pre><code>length: 12
  format: every 4 digits "-"
  some letters should be excluded like: O or l
  </code></pre>
  
  <p>The codes should be "secure" (i.e totally random) and it should be possible to run the script multiple times in case the codes aren't enough and we have to create more codes.</p>
  
  <p>e.g. <code>ab4D-406a-BCh7-TEs3</code></p>
  
  <p>I have to solve this in Python 3.</p>
  
  <p>My first idea was to save the codes into an database and just create them with the random function ASCII-Code -> Letter but maybe the scirpt creates the same code twice so I habe to check every time if that code already exists in the database which will cause a lot of database traffic.</p>
  
  <p>My second idea is to use a hash function, but I think the codes wouldn't be secure and there are no hashfunctions which pass my rules.</p>
  
  <p>My third idea is to use somethink like a random module from python to create the code and write the codes into an file and check the file every time if the code is already inside. But that's also not good for performance but I think betther than using a database.</p>
  
  <p>Anybody an idea how to solve that problem with high performance?</p>
  
  <p>Greetings.</p>
  
  <p>Edit:</p>
  
  <p>I tried this but  it takes hours to create the codes. Some tipps how to increase the performance?</p>
  
  <pre><code>import random
  
  sequence = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
  seq = list(sequence)
  
  
  codelist = []
  counter = 0
  while len(codelist) &lt; 5000000:
      code = ""
      counter = counter +1
      print(counter)
      while len(code) &lt; 12:
          code = code + str(random.choice(seq))
  
      try:
          codelist.index(code)
      except ValueError:
          codelist.append(code)
  
  
  file = open('codefile.txt','w')
  for item in codelist:
      file.write("%s\n" % item)
  </code></pre>