Home > database >  Python: cannot iterate over elements, if several identical elements stay in a row
Python: cannot iterate over elements, if several identical elements stay in a row

Time:10-26

I'm a beginner, so sorry for such question.

I would like to iterate over the array and find unique values. However, it doesn't iterate further than "A". How could I solve it?

Thank you a lot in advance! Code ->

def letters_learned_today(word):
    unique_letters = []

    for letter in word:
        already_exists = False
        if letter in unique_letters:
            already_exists = True
            break
        else:
            unique_letters.append(letter)
    return unique_letters

word = 'AAABBVSCDDDERZAUJJFFPOUSST'
print(letters_learned_today(word)) 

CodePudding user response:

Here is the answer:

def letters_learned_today(word):
    unique_letters = []

    for letter in word:
        if letter not in unique_letters:
            unique_letters.append(letter)
    return unique_letters


word = "AAABBVSCDDDERZAUJJFFPOUSST"
print(letters_learned_today(word))
  • Related