Junior — Middle
How can you iterate through list elements using a while loop?
sobes.tech AI
Answer from AI
To iterate over list elements using a while loop, you can use an index to access the elements. Example:
my_list = ['a', 'b', 'c', 'd']
i = 0
while i < len(my_list):
print(my_list[i])
i += 1
Here, we initialize the counter i to zero, then in the loop, we print the element at index i and increment i by 1 until we have traversed the entire list.