Newbie: last item of a loop

Sean 'Shaleh' Perry shalehperry at attbi.com
Fri May 2 09:31:36 EDT 2003


>
> I have a list of names which I want as a joined string, seperated by
> kommas except for the last item which is seperated by 'and'. ['Peter',
> 'Paul','Mary'] -> 'Peter, Paul and Mary'
>
> here is what I tried (I know that the 'i.index' does not give me the
> index-number of the sequence item, but I don't know better. Sorry. But I
> hope you get the point ...) :
>

this is also useful for making csv data.

The usual tactic as others have shown is to process the interesting item 
outside the loop.  This helps make the desire clear for other readers of your 
code and is often more efficient.

Thought I would add a direct comment as to the why and wherefore of the 
solution.





More information about the Python-list mailing list