How to find the next index of a charachter in python list

Question:

I was dealing with python list and I want to find the next index of the recurring character in the list how can I accomplish that

country = ['Beekeeper', 'Fly', 'Hornet']


pickedCountry = random.choice(country)
let the pickedCountry = 'Beekeeper'
print(pickedCountry .index('e'),)

it print out only 1

Asked By: Suretion

||

Answers:

pickedCountry = 'Beekeeper'
last_index = 0
while last_index < len(pickedCountry):
    current_index = pickedCountry.index('e', last_index)
    print(current_index)
    last_index = current_index + 1
pickedCountry = 'Beekeeper'
last_index = 0
while last_index < len(pickedCountry):
    try:
        current_index = pickedCountry.index('e', last_index)
    except ValueError:
        break
    print(current_index)
    last_index = current_index + 1
Answered By: 吴慈霆
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.