Yethrosh Yethrosh - 1 year ago 110
Python Question

How to auto-increment in python

I am new to python. I want to print all available functions in os module as:

1 functionName

2 functionName

3 functionName

and so on.

Currently I am trying to get this kind of output with following code:

import os

cur = dir(os)
for i in cur:
count = 1
count += 1

But it prints as below:

2 functionName

2 functionName

2 functionName

till end.

Please help me generate auto increment list of numbers, Thanks.

Answer Source

Use enumerate

import os

cur = dir(os)
for i, f in enumerate(cur, start=1):
    print("%d: %s" % (i, f))
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download