title: Remove item from list based on the next item in same list
body: <p>I just started learning python and here I have a sorted list of protein sequences (total 59,000 sequences) and some of them overlap. I have made a toy list here for example:</p>
  
  <pre><code>ABCDE
  ABCDEFG
  ABCDEFGH
  ABCDEFGHIJKLMNO
  CEST
  DBTSFDE
  DBTSFDEO
  EOEUDNBNUW
  EOEUDNBNUWD
  EAEUDNBNUW
  FEOEUDNBNUW
  FG
  FGH
  </code></pre>
  
  <p>I would like to remove those shorter overlap and just keep the longest one so the desired output would look like this:</p>
  
  <pre><code>ABCDEFGHIJKLMNO
  CEST
  DBTSFDEO
  EAEUDNBNUW
  FEOEUDNBNUWD
  FGH
  </code></pre>
  
  <p>How can I do it? My code looks like this:</p>
  
  <pre><code>with open('toy.txt' ,'r') as f:
      pattern = f.read().splitlines()
      print pattern
  
      for i in range(0, len(pattern)):
          if pattern[i] in pattern[i+1]:
              pattern.remove(pattern[i])
          print pattern
  </code></pre>
  
  <p>And I got the error message:</p>
  
  <pre><code>['ABCDE', 'ABCDEFG', 'ABCDEFGH', 'ABCDEFGHIJKLMNO', 'CEST', 'DBTSFDE', 'DBTSFDEO', 'EOEUDNBNUW', 'EAEUDNBNUW', 'FG', 'FGH']
  ['ABCDEFG', 'ABCDEFGH', 'ABCDEFGHIJKLMNO', 'CEST', 'DBTSFDE', 'DBTSFDEO', 'EOEUDNBNUW', 'EAEUDNBNUW', 'FG', 'FGH']
  ['ABCDEFG', 'ABCDEFGHIJKLMNO', 'CEST', 'DBTSFDE', 'DBTSFDEO', 'EOEUDNBNUW', 'EAEUDNBNUW', 'FG', 'FGH']
  ['ABCDEFG', 'ABCDEFGHIJKLMNO', 'CEST', 'DBTSFDE', 'DBTSFDEO', 'EOEUDNBNUW', 'EAEUDNBNUW', 'FG', 'FGH']
  ['ABCDEFG', 'ABCDEFGHIJKLMNO', 'CEST', 'DBTSFDEO', 'EOEUDNBNUW', 'EAEUDNBNUW', 'FG', 'FGH']
  ['ABCDEFG', 'ABCDEFGHIJKLMNO', 'CEST', 'DBTSFDEO', 'EOEUDNBNUW', 'EAEUDNBNUW', 'FG', 'FGH']
  ['ABCDEFG', 'ABCDEFGHIJKLMNO', 'CEST', 'DBTSFDEO', 'EOEUDNBNUW', 'EAEUDNBNUW', 'FG', 'FGH']
  ['ABCDEFG', 'ABCDEFGHIJKLMNO', 'CEST', 'DBTSFDEO', 'EOEUDNBNUW', 'EAEUDNBNUW', 'FGH']
  Traceback (most recent call last):
    File "test.py", line 8, in &lt;module&gt;
      if pattern[i] in pattern[i+1]:
  IndexError: list index out of range
  </code></pre>