I'm sure that this has been asked before, but I've tried about a half a dozen keywords and I couldn't find it.
I have this for loop that appends a letter to a list if it doesn't exist:
word_list = ['cat', 'dog', 'rabbit']
letter_list = []
for word in word_list:
for letter in word:
if letter not in letter_list:
letter_list.append(letter)
print(letter_list)
# ['c', 'a', 't', 'd', 'o', 'g', 'r', 'b', 'i']
I'm trying to do the same thing using list comprehension, but I don't know how to have my if statement test the letter_list_comp
before it is defined. This is what I want to do:
letter_list_comp = [letter for word in word_list for letter in word if letter not in letter_list_comp]