title: How can I speed up an iteration in python?
body: <p>I was wondering if you could help me to speed up my python script.</p>
  
  <p>I have two lists:</p>
  
  <pre><code>a=['a','b','c','d','e','f','g','h','i','j']
  
  b=['b','f','g','j']
  </code></pre>
  
  <p>I want to create a list that will contain elements of b, but will have a length of a, with elements not in b replaced by something else, let's say <code>'-999'</code>. Also, instead of having the actual elements (a,b,c...) I want to substitute that with the element's index from b. So it would look like that:</p>
  
  <pre><code>c=['-999',0,'-999','-999','-999', 1, 2,'-999','-999',3] 
  </code></pre>
  
  <p>My code for now is:</p>
  
  <pre><code>c=[]
  
  counter=0
  
  for each in a:
      if each in b:
          c.append(counter)
          counter+=1
      else:
          c.append('-999')
  </code></pre>
  
  <p>It works fine, however, in real life my list a is 600 000 elements long, and there are actually 7 b lists that I need to iterate them over, all between 3k and 250k elements as well. </p>
  
  <p>Any ideas on how to speed this up? </p>