looping theough a list code example
Example 1: 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 2: how to loop through a list
List<int> list = new List<int> { 1, 2, 3, 4, 5 };
for (int i = 0; i < list.Count(); i++)
{ Console.WriteLine(list[i]); }
foreach (int item in list)
{ Console.WriteLine(item); }