Small problem with print and comma

Diez B. Roggisch deets at nospam.web.de
Mon Jul 31 03:58:56 EDT 2006


Dustan wrote:

> Dennis Lee Bieber wrote:
>> >     for i in range(0,len(param)):
>> > print a[i],
>>
>> for it in param:
>> print it,
> 
> That's one way. However, if you need the position (this is for future
> reference; you don't need the position number here):
> 
> for i in range(len(param)+1):
>     print a[i],
> 
> The last position was excluded because you forgot the '+1' part,
> creating an off-by-one bug.

No, your code creates that bug.

However, the above is not very pythonic - if param is a iterator and not a
sequence-protocol-adherent object, it fails. The usual way to do it is


for i, a in enumerate(param):
    print a,


Diez



More information about the Python-list mailing list