New to python but have java experience. I was watching a fcc youtube video, and the bolded line in the code snippets below really bothers me. I understand what it does, but I don’t understand the syntax. It is just not logical. Is there another way to rewrite it in a single line? Essentially the line is iterating thru characters in the ‘word’ variable, and if the character is in the ‘used_letters’ list, then add it to a new list, otherwise add ‘-’ to the new list.
word =‘APPLE’
used_letters=[‘A’,‘L’] word_list = [letter if letter in used_letters else ‘-’ for letter in word]
print(’ '.join(word_list))
You can add brackets: word_list = [(letter if letter in used_letters else ‘-’) for letter in word]
That’s just the syntax of list-comprehension and inline if-else. It get’s even weirder if you make nested list-comprehensions… but that’s just the syntax.