I was wondering what would be the syntax of a double for loop into a list comprehension which have an if and else statement used in the first for loop. So this question born from this function that I made, where I have one list comprehension with two loops but the if and else statements are in the second loop:
def double_for_list_comp_ifelse_second_loop(lst):
return [(j if j == 1 else 0) for i in lst for j in i]
l = [[0,1],[0,3]]
double_for_list_comp_ifelse_second_loop(lst=l)
output: [0, 1, 0, 0]
The list comprehension should compress this syntax:
for i in lst:
if...
else...
for j in i:
if...
else...
Aucun commentaire:
Enregistrer un commentaire