how to loop over list 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: iterate through a list
my_list = ["Hello", "World"]
for i in my_list:
print(i)
Example 4: how to iterate over a list in python
lst = [10, 50, 75, 83, 98, 84, 32]
res = list(map(lambda x:x, lst))
print(res)