text: user856: I have following two lists.
  List1
  m64041_200717_231916/100664277/0_42364 id=2440
  m64041_200717_231916/100008029/0_42112 id=244006
  m64041_200717_231916/100665802/0_46854 id=2440067
  m64041_200717_231916/100008044/0_60161 id=24400
  m64041_200717_231916/100928073/0_11804 id=2440152
  m64041_200717_231916/10094492/0_44157 id=2440199
  m64041_200717_231916/100008373/4102_50601 id=2440234
  
  List2
  m64041_200717_231916/100664277/0_42364
  m64041_200717_231916/100665802/0_46854
  m64041_200717_231916/100928073/0_11804
  m64041_200717_231916/10094492/0_44157
  
  These are sequence read ids from a genome file. List1 contains all the ids from the List2 but with some id numbers (id=2440). So the sequence ids are only similar to some extent. I want to compare these two files and I need to filter out List2 sequence ids from List one (with id numbers) as below.
  Output.
  m64041_200717_231916/100664277/0_42364 id=2440
  m64041_200717_231916/100665802/0_46854 id=2440067
  m64041_200717_231916/100928073/0_11804 id=2440152
  m64041_200717_231916/10094492/0_44157 id=2440199
  
  I will be grateful if someone can help me with this issue.
  
  user-1: There is no need for a highlevel language in such a simple case, an awk oneliner does just fine:
  $ awk -F " " 'FNR==NR {a[$1]; next} $1 in a' List2 List1
  m64041_200717_231916/100664277/0_42364 id=2440
  m64041_200717_231916/100665802/0_46854 id=2440067
  m64041_200717_231916/100928073/0_11804 id=2440152
  m64041_200717_231916/10094492/0_44157 id=2440199
  
  
  user7153: You really need to learn some python :)
  with open('/path/list2.txt', 'r') as fin:
      to_keep = set([])
      for line in fin:
          to_keep.add(line.strip())
          
  with open('/path/list1.txt', 'r') as fin:
      with open('/path/list3.txt', 'w') as fout:
          for line in fin:
              if line.strip().split(' ')[0] in to_keep:
                  fout.write(line)
  
  
original_text: 856: <p>I have following two lists.
  List1</p>
  <pre><code>m64041_200717_231916/100664277/0_42364 id=2440
  m64041_200717_231916/100008029/0_42112 id=244006
  m64041_200717_231916/100665802/0_46854 id=2440067
  m64041_200717_231916/100008044/0_60161 id=24400
  m64041_200717_231916/100928073/0_11804 id=2440152
  m64041_200717_231916/10094492/0_44157 id=2440199
  m64041_200717_231916/100008373/4102_50601 id=2440234
  </code></pre>
  <p>List2</p>
  <pre><code>m64041_200717_231916/100664277/0_42364
  m64041_200717_231916/100665802/0_46854
  m64041_200717_231916/100928073/0_11804
  m64041_200717_231916/10094492/0_44157
  </code></pre>
  <p>These are sequence read ids from a genome file. List1 contains all the ids from the List2 but with some id numbers (id=2440). So the sequence ids are only similar to some extent. I want to compare these two files and I need to filter out List2 sequence ids from List one (with id numbers) as below.
  Output.</p>
  <pre><code>m64041_200717_231916/100664277/0_42364 id=2440
  m64041_200717_231916/100665802/0_46854 id=2440067
  m64041_200717_231916/100928073/0_11804 id=2440152
  m64041_200717_231916/10094492/0_44157 id=2440199
  </code></pre>
  <p>I will be grateful if someone can help me with this issue.</p>
   -1: <p>There is no need for a highlevel language in such a simple case, an <code>awk</code> oneliner does just fine:</p>
  <pre><code><span class="math-container">$ awk -F " " 'FNR==NR {a[$1]; next} $</span>1 in a' List2 List1
  m64041_200717_231916/100664277/0_42364 id=2440
  m64041_200717_231916/100665802/0_46854 id=2440067
  m64041_200717_231916/100928073/0_11804 id=2440152
  m64041_200717_231916/10094492/0_44157 id=2440199
  </code></pre>
   7153: <p>You really need to learn some python :)</p>
  <pre><code>with open('/path/list2.txt', 'r') as fin:
      to_keep = set([])
      for line in fin:
          to_keep.add(line.strip())
          
  with open('/path/list1.txt', 'r') as fin:
      with open('/path/list3.txt', 'w') as fout:
          for line in fin:
              if line.strip().split(' ')[0] in to_keep:
                  fout.write(line)
  </code></pre>
  