Junior — Middle
How can you iterate over list elements in Python without using a for loop and built-in iterator?
sobes.tech AI
Answer from AI
Recursion can be used to traverse list elements without a for loop and built-in iterator. For example:
def recursive_print(lst, index=0):
if index < len(lst):
print(lst[index])
recursive_print(lst, index + 1)
my_list = [1, 2, 3, 4]
recursive_print(my_list)
Here, the function calls itself, iterating through the list elements by index.