I'd like to know the best way (more compact and "pythonic" way) to do a special treatment for the last element in a for loop. There is a piece of code that should be called only between elements, being suppressed in the last one. Here is how I currently do it:
for i, data in enumerate(data_list):
code_that_is_done_for_every_element
code_that_is_done_for_every_element
code_that_is_done_for_every_element
if i != len(data_list) - 1:
code_that_is_done_between_elements
code_that_is_done_between_elements
code_that_is_done_between_elements
Is there any better way?
Note: I don't want to make it with hacks such as using reduce
;)