Build 2 lists in one go while reading from file, pythonically
<p>I'm reading a big file with hundreds of thousands of number pairs representing the edges of a graph. I want to build 2 lists as I go: one with the forward edges and one with the reversed. </p>

<p>Currently I'm doing an explicit <code>for</code> loop, because I need to do some pre-processing on the lines I read. However, I'm wondering if there is a more pythonic approach to building those lists, like list comprehensions, etc. </p>

<p>But, as I have 2 lists, I don't see a way to populate them using comprehensions without reading the file twice.</p>

<p>My code right now is:</p>

<pre><code>with open('SCC.txt') as data:
    for line in data:
        line = line.rstrip()
        if line:
            edge_list.append((int(line.rstrip().split()[0]), int(line.rstrip().split()[1])))
            reversed_edge_list.append((int(line.rstrip().split()[1]), int(line.rstrip().split()[0])))
</code></pre>

<p>Maybe not clearer, but shorter:</p>

<pre><code>with open('SCC.txt') as data:
    process_line = lambda line, r: (int(line.rstrip().split()[r]), int(line.rstrip().split()[1-r]))

    edge_list, reverved_edge_list = map(list, zip(*[(process_line(line, 0), process_line(line, 1)) 
                                                    for line in data
                                                    if line.rstrip()]))
</code></pre>

python list python-3.x