title: Extracting part of list from another list
body: <p>I have two lists. One list just has elements in a random order (<code>y</code>), while another ordered list has list subsets (<code>x</code>). The first elements of those list subsets are the same as the random list.</p>
  
  <p>I want all the elements from the second list which are present in the first list, but in the same order. Here is an example:</p>
  
  <p>Input:</p>
  
  <pre><code>x = [[1,'a'],[2,'b'],[3,'c'],[4,'d'],[5,'d']]
  y = [4,1,2]
  </code></pre>
  
  <p>Required Output:</p>
  
  <pre><code>[[4, 'd'], [1, 'a'], [2, 'b']] # Same sequence as y but elements in x
  </code></pre>
  
  <p>I have written some code to accomplish this:</p>
  
  <pre><code>for i in range(0,len(y)):
      for k in range(0,len(x)):
          if y[i] ==x[k][0]:
              d.append(x[k])
  </code></pre>
  
  <p>However, I am not sure if this is the most efficient way to go about this. This is just an example, in reality, my list <code>x</code> contains over a million sub lists and so if I try to use this code, it will loop over <code>x</code> a million times. </p>
  
  <p>How can I make this code more efficient?</p>