Python: For Loops

(Last Updated On: )

There are a variety of different ways to write a for loop in python. See below for different options. If you have any other suggestions please feel to add a comment.

Loop through a list one record at a time.

for rec in data:
	#Do Something

Loop through a range of numbers up to 100.

for i in range(100)
	#Do Something

Enumerate a list getting the index and value.

for idx, value in enumerate(data):
	#Do Something

Inline for loop.

d = [ x for x in data]

Inline for loop with an if condition.

d = [ x for x in data if x==1]