Junior — Middle
How can you iterate over list elements during program execution?
sobes.tech AI
Answer from AI
In Python, to iterate over list elements, a for loop is usually used. For example:
my_list = [1, 2, 3, 4, 5]
for element in my_list:
print(element)
You can also use a while loop with an index:
index = 0
while index < len(my_list):
print(my_list[index])
index += 1
For more complex cases, you can use the enumerate() function to get both index and value at the same time:
for index, element in enumerate(my_list):
print(f"Index {index}: {element}")