Python: Running a for loop with multiple inputs and sorting the results into different lists
<p>So I have a bit of code that looks like this:</p>

<pre><code>measure_list=[]
for file in file_list:
    with open(file,"r") as read_data:
        y=read_data.read()
        if "Part: ABCD" in y:
            for line in m:
                if "Measure X:" in line:
                    measure_list.append(line)
                elif "Measure Y:" in line:
                    measure_list.append(line)

final=''.join(measure_list).replace("Measure","\nMeasure")
print(final)
</code></pre>

<p>(This last part just helps organize the output)</p>

<p>So this part of the code opens a group of files and scans each one to see if it is "Part: ABCD", and if it is, it will pull the lines for "Measure X:" and "Measure Y:" and add them to the measure_list. My problem is that I have a lot of files and there will be multiple "Part: ABCD" files. And after joining the list with .join() the output will then look like:</p>

<pre><code>Part: ABCD
Measure X: (Numbers)
Measure Y: (Numbers)
Measure X: (Numbers)
Measure Y: (Numbers)
Measure X: (Numbers)
Measure Y: (Numbers)
</code></pre>

<p>My question is if anybody knows a way to organize the output so that it looks like so:</p>

<pre><code>Part: ABCD
Measure X: (Numbers)
Measure X: (Numbers)
Measure X: (Numbers)
Measure Y: (Numbers)
Measure Y: (Numbers)
Measure Y: (Numbers)
</code></pre>

<p>Any help is appreciated.</p>

<p>Make two arrays of outputs and sort the input into each using a for loop with an if statement inside. Then join the two outputs.</p>

python python-3.x