0

Is it possible to find the index of a list in a for loop without using a generator?

for character in string:
    if character == "a":
        print(get_index_somehow)

I am aware that you could use a generator in a for loop and get the index that way

for i in range(len(string)):
    character = string[i]
        if character == "a":
            print(i)

Just curious if it's possible

Skkkitzo
  • 16
  • 1
  • 1
    Beside the point, but `range` is an iterator, not a generator. `enumerate` is also an iterator. In any case I'm not sure why you wouldn't want to use an iterator/generator. BTW welcome to SO! Check out the [tour] and [ask]. – wjandrea May 28 '20 at 15:21
  • @takendarkk Bingo. Thanks very much. No matter how experienced you are there's always something to learn! – Skkkitzo May 28 '20 at 15:23

0 Answers0