title: Efficiency question: how to compare two huge nested lists and make changes based on criteria
body: <p>I want to compare two huge identical nested lists and by iterating over both of them. I'm looking for nested lists in where list_a[0] is equal to list_b[1]. In that case I want to merge those lists (the order is important). The non-matches lists I also want in the output.</p>
  <pre><code>rows_a = [['a', 'b', 'z'], ['b', 'e', 'f'], ['g', 'h', 'i']]  
  rows_b = [['a', 'b', 'z'], ['b', 'e', 'f'], ['g', 'h', 'i']]
  
  data = []  
  
  for list_a in rows_a:  
      for list_b in rows_b:  
          if list_a[0] == list_b[1]:  
              list_b.extend(list_a)     
              data.append(list_b)  
          else:  
              data.append(list_b)
  
  #print(data): [['a', 'b', 'z', 'b', 'e', 'f'], ['b', 'e', 'f'], ['g', 'h', 'i'], ['a', 'b', 'z', 'b', 'e', 'f'], ['b', 'e', 'f'], ['g', 'h', 'i'], ['a', 'b', 'z', 'b', 'e', 'f'], ['b', 'e', 'f'], ['g', 'h', 'i']]  
  </code></pre>
  <p>Above is the output that I do <strong>NOT</strong> want, because it is way too much data. All this unnecessary data is caused by the double loop over both rows. A solution would be to slice an element off rows_b by every iteration of the for loop on rows_a. This would avoid many duplicate comparisons. <strong>Question:</strong> How do I skip first element of a list every time it has looped from start to end?</p>
  <p>In order to show the desired outcome, I correct the outcome by deleting duplicates below:</p>
  <pre><code>res=[]
  for i in data:
      if tuple(i) not in res:
          res.append(tuple(i))
          
  print(res)
  
  #Output: [('a', 'b', 'z', 'b', 'e', 'f'), ('b', 'e', 'f'), ('g', 'h', 'i')]  
  </code></pre>
  <p>This is the output I want! But faster...And preferably without removing duplicates.</p>
  <p>I managed to get what I want when I work with a small data set. However, I am using this for a very large data set and it gives me a 'MemoryError'. Even if it didn't give me the error, I realise it is a very inefficient script and it takes a lot of time to run.</p>
  <p>Any help would be greatly appreciated.</p>