Fastest way to print this in python?
<p>Suppose I have a text file containing this, where the number on the left says how many of the characters of the right should be there:   </p>

<pre><code>2 a
1 *
3 $
</code></pre>

<p>How would I get this output in the fastest time?  </p>

<pre><code>aa*$$$
</code></pre>

<p>This is my code, but has N^2 complexity:  </p>

<pre><code>f = open('a.txt')
for item in f:
    item2=item.split()
    num = int(item2[0])
    for i in range(num):
        line+=item2[1]
print(line)
f.close()
</code></pre>

<p>Your code is actually O(sum(n_i)) where n_i is the number in the row i. You can't do any better and none of the solutions in the other answers do, even if they might be faster than yours.</p>

python