idiom for list looping

MRAB python at mrabarnett.plus.com
Wed Jul 29 05:38:29 EDT 2009


superpollo wrote:
> 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?
> 
'enumerate' is the Pythonic solution.



More information about the Python-list mailing list