Newbie: last item of a loop

Sven Brandt sven_NOSPAM at manastar.de
Fri May 2 08:50:48 EDT 2003


Sorry, I mixed up some of the variable names. Here is the correct code:

my_names=['Peter', 'Paul','Mary']
my_name_string=''

for i in my_names:
   # if more than one name
   if len(my_names) > 1:
     # if not last or second-last name, append komma
     if i.index  < len(my_names) - 1:
       my_name_string = my_name_string + ', '
     # if second-last append 'and'
     if i.index  == len(my_names) - 1:
       my_name_string = my_name_string + ' and '


Sven






More information about the Python-list mailing list