Sorting a Dictionary

Ben Skelton skeltobc at mailandnews.com
Tue Mar 14 11:17:06 EST 2000


On Tue, 14 Mar 2000, Remco Gerlich wrote:
>> for i in range(len(l)):
>> 		print l[i]
>
>But but but... why not simply
>
>for i in l:
>   print i
>
>That idiom range(len(l)) is only useful if you're actually doing something
>with the index, other than using it as an index...

I sometime have to use the first form if the elements of the list are being
modified...it's annoying and maybe I've missed something:

>>> a = ['one', 'two', 'three']
>>> for x in a:
...     x = x + '***'
...
>>> print a
['one', 'two', 'three']
>>> for i in range(len(a)):
...     a[i] = a[i] + '***'
...
>>> print a
['one***', 'two***', 'three***']                                                      

--Ben





More information about the Python-list mailing list