idiom for list looping

superpollo user at
Wed Jul 29 11:02:25 CEST 2009

hi clp.

i want to get from here:

nomi = ["one", "two", "three"]

to here:

0 - one
1 - two
2 - three

i found two ways:

first way:

for i in range(len(nomi)):
     print i, "-", nomi[i]

or second way:

for (i, e) in enumerate(nomi):
     print i, "-", e

which one is "better"? is there a more pythonic way to do it?



max2 at$ python -V
Python 2.3.4
max2 at$ uname -a
Linux fisso 2.4.24 #1 Thu Feb 12 19:49:02 CET 2004 i686 GNU/Linux
max2 at$

More information about the Python-list mailing list