Given an array called array, loop through all of its elements and print them out.
I have tried to use the following code but it is giving me the error TypeError: list indices must be integers, not str:
for element in array:
print(array[element])
I have found the article Accessing the index in 'for' loops and I’ve tried to implement it like so:
for element, x in enumerate(array):
print(array[element])
This still does not print what I was searching for.
>Solution :
Using normal for loop if you only need the elements of the array:
for element in array:
print(element)
Using enumerate if you need the element and the index:
for index, element in enumerate(array):
print(f"index {index} has element: {element}")