Is there a way to enumerate in list comprehension where the enumeration only increments when the if statement is True?
For example, the code below enumerates whether the if statement is True or False and I would like to have a continuous index.
my_list = [0,1,2] [i for i, w in enumerate(my_list) if w != 1]
I also tried the following, but you can't use pass inside a list comprehension like below.
[i if w != 1 else pass for i, w in enumerate(my_list)]
Aucun commentaire:
Enregistrer un commentaire