'I'm making a hangman project but can't figure out how this piece of code works [duplicate]

I want to know what letter if letter means in this code.

I have no variable that's called like that, so how can this be valid? And how does this work in general?

word_list = [letter if letter in used_letters else "-" for letter in word]


Solution 1:[1]

word_list = [letter if  letter in used_letters else "-" for letter in word]

Here 'letter if letter' is a variable created for the sake of list comprehension ( it works inside the square brackets i.e. [ ] and we cannot access the value inside this variable outside this brackets )in python so that we can use this variable to filter out if this letter is in the used_letters list.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 raiyan22