How to treat the last element in list differently in Python?

Posted by prosseek on Stack Overflow See other posts from Stack Overflow or by prosseek
Published on 2010-03-11T22:15:02Z Indexed on 2010/03/12 1:17 UTC
Read the original article Hit count: 204

Filed under:

I need to do some special operation for the last element in a list. Is there any better way than this?

array = [1,2,3,4,5] 
for i, val in enumerate(array): 
  if (i+1) == len(array): 
    // Process for the last element 
  else: 
    // Process for the other element 

© Stack Overflow or respective owner

Related posts about python