mercredi 3 novembre 2021

Why I can't use pass in python if else statement when use condition in a single line

I want to check if an item is in a list and if not, I want to append it to the list.

Usually I would write it:

if item not in list:
    list.append(item)
else:
    pass

However, I got to the point where I try to keep my code shorter and got to this:

list.append(item) if item not in list else list

To be fair, only list.append(item) if item not in list is my own creation. The else statement is due to insistence of PyCharm. Now to my question: Why can't I follow up the else statement with pass but must write list instead. I can't wrap my head around it, nor did Google help too.

Thanks for your clarifications.

Aucun commentaire:

Enregistrer un commentaire