(Jul-07-2021, 10:58 AM)ibreeden Wrote: I find this the most elegant way to process the list:
the_list = [1,2,3,4] for i in the_list[0:-1]: print(f"Processing {i}") print(f"Post processing {the_list[-1]}")
Output:Processing 1 Processing 2 Processing 3 Post processing 4
for cases where the entire body of the loop changes, i agree that is the best way. but if the last cycle needs to change only a portion, more so the smaller that portion is, i would prefer to avoid any duplicated code and have every cycle performed by the same body code that tests for the case of the last cycle. if the common part of that code ever needs to be changed or trapped for debugging, avoiding duplication makes code maintenance easier (so there is never an issue of what needs to be the same actually being the same).
Tradition is peer pressure from dead people
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.