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>You can multiply strings to repeat them in Python:</p>

<p><code>"foo" * 3</code> gives you <code>foofoofoo</code>.</p>

<pre><code>line = []
with open("a.txt") as f:
    for line in f:
        n, c = line.rstrip().split(" ")
        line.append(c * int(n))

print("".join(line))
</code></pre>

<p>You can print directly but the code above lets you get the output you want in a string if you care about that.</p>

<p>Using a list then <code>join</code>ing is more efficient than using <code>+=</code> on a string because strings are immutable in Python. This means that a new string must be created for each <code>+=</code>. Of course printing immediately avoids this issue.</p>

python