how to loop a list in python code example
Example 1: python loop through list
list = [1, 3, 6, 9, 12]
for i in list:
print(i)
Example 2: how to loop over list
# Python code to iterate over a list
list = [1, 2, 3, 4, 5, 6]
# Method 1: Using "var_name in list" syntax
# Pro: Consise, easily readable
# Con: Can't access index of item
for item in list:
print(item)
# Method 2: Using list indices
# Pro: Can access index of item in list
# Con: Less consise, more complicated to read
for index in range(len(list)-1):
print(list[index])
# Method 3: Using enumerate()
# Pro: Can easily access index of item in list
# Con: May be too verbose for some coders
for index, value in enumerate(list):
print(value)
Example 3: python iterate list
lst = [10, 50, 75, 83, 98, 84, 32]
for x in range(len(lst)):
print(lst[x])
Example 4: python iterate through list
for row in list: