Print the current number of item in a cycle for x in y
<p>I have this script:</p>

<pre><code>accounts = open("accounts.txt").readlines()

y = [x.strip().split(":") for x in accounts]

for account in y:
    print ("Trying with: %s:%s" % (account[0], account[1]))
</code></pre>

<p>the file accounts.txt is structred like this:</p>

<pre><code>email1@email.com:test1
email2@email.com:test2
email3@gmail.com:test3
</code></pre>

<p>How can i add to the print "Trying with... bla bla" the current line of the account? An output like:</p>

<pre><code>Trying with: email1@email.com:test1 @1
Trying with: email2@email.com:test1 @2
Trying with: email3@email.com:test1 @3
</code></pre>

<p>You can use <a href="https://docs.python.org/3/library/functions.html#enumerate" rel="nofollow"><code>enumerate()</code></a>, with the <code>start</code> argument as suggested by @JonClements:</p>

<pre><code>for i, account in enumerate(y, start=1):
    print ("Trying with: %s:%s @%d" % (account[0], account[1], i))
</code></pre>

<p>You can also use unpacking to make the line more readable:</p>

<pre><code>for i, (mail, name) in enumerate(y, start=1):
    print ("Trying with: %s:%s @%d" % (mail, name, i))
</code></pre>

<p>Finally, as @idjaw notified it, there is <a href="https://docs.python.org/3/library/stdtypes.html#str.format" rel="nofollow">a better way to format string</a> which is recommended over the old style:</p>

<pre><code>for i, (mail, name) in enumerate(y, start=1):
    print("Trying with: {}:{} @{}".format(mail, name, i))
</code></pre>

python list python-3.x for-loop