Is there any simple way to find the Last Iteration of the for Loop in Python? I just want to convert a list to CSV.
            Asked
            
        
        
            Active
            
        
            Viewed 3.7k times
        
    7
            
            
        - 
                    6No. But maybe if you explain your actual problem, we can suggest an alternative solution? – Björn Pollex Mar 08 '11 at 10:17
- 
                    2I wonder if the standard csv module might solve your problem another way. – Martin Stone Mar 08 '11 at 10:38
- 
                    1I'm not sure why none of the replies to this question actually answer it... – 2rs2ts May 30 '13 at 15:45
- 
                    Here's an actual answer for the generic situation.. http://stackoverflow.com/a/1630350/804616 – trss Jul 13 '14 at 09:45
- 
                    "Is there any simple way to find the Last Iteration of the for Loop in Python? I just want to convert a list to CSV." These are two separate questions, both of which have much better versions, so I closed this as duplicates of both. Aside from that, it is not at all clear *why knowing the "last iteration" would help" in the conversion task. – Karl Knechtel Mar 29 '23 at 05:47
5 Answers
13
            To convert a list to CSV, use the join-function:
>>> lst = [1,2,3,4]
>>> ",".join(str(item) for item in lst)
"1,2,3,4"
If the list already contains only string, you just do ",".join(l).
 
    
    
        Björn Pollex
        
- 75,346
- 28
- 201
- 283
- 
                    16this will not properly escape values containing ',' for that use the csv module – Dan D. Mar 08 '11 at 10:40
13
            
            
        Your best solution is probably to use the csv module, as suggested elsewhere. However, to answer your question as stated:
Option 1: count your way through using enumerate()
for i, value in enumerate(my_list):
    print value,
    if i < len(my_list)-1:
        print ", followed by"
Option 2: handle the final value (my_list[-1]) outside the loop
for value in my_list[:-1]:
    print value, ", followed by"
print my_list[-1]
 
    
    
        Martin Stone
        
- 12,682
- 2
- 39
- 53
10
            
            
        actually when a for loop in python ends the name that it bound is still accessible and bound to its last value:
for i in range(10):
    if i == 3:
        break
print i # prints 3
i use this trick with with like:
with Timer() as T:
    pass # do something
print T.format() # prints 0.34 seconds
 
    
    
        Dan D.
        
- 73,243
- 15
- 104
- 123
5
            
            
        Not exactly what you want:
>>> for i in range(5):
    print(i)
else:
    print("Last i is",i)
0
1
2
3
4
Last i is 4
Edited: There is csv module in standard library, or simply ','.join(LIST)
 
    
    
        Kabie
        
- 10,489
- 1
- 38
- 45
- 
                    2`for`'s `else` only runs if the loop exits at the end of the iterator not via `break` – Dan D. Mar 08 '11 at 10:28
- 
                    
 
    