Yethrosh Yethrosh - 1 month ago 6
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
print(count,i)


But it prints as below:

2 functionName

2 functionName

2 functionName


till end.

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

Answer

Use enumerate

import os

cur = dir(os)
for i, f in enumerate(cur, start=1):
    print("%d: %s" % (i, f))
Comments