idiom for list looping

superpollo user at example.net
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?

bye

ps:

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



More information about the Python-list mailing list