List comprehension in Python : if else not working
Question:
I’m trying to add number of False in a list equivalent to a variable number, working if the variable is not 0. I’m trying to add a "else" statement if the variable is 0 but it’s not working.
Here is what I tired :
floors = 5
blocked_floor = [False for i in range(floors) if (floors > 0) else False]
blocked_floor1 = [False for i in range(floors) if (floors > 0) else False for i in range(1)]
There are a lot of topics about that issue but I tried everything that was in, nothing worked. I have a synthax error on the "else".
Do you have any idea about the issue ?
Answers:
Your syntax is indeed wrong.
Instead of:
blocked_floor1 = [False for i in range(floors) if (floors > 0) else False for i in range(1)]
You wanted:
blocked_floor1 = [False for i in range(floors) if floors > 0] if floors > 0 else False
Or:
blocked_floor1 = [False for i in range(floors) if floors > 0] if floors > 0 else [False]
The difference being that in the first case, blocked_floor1
will be False
, and in the second case it will be [False]
. I’d think the first case is preferable, since otherwise you won’t be able to tall if floors was 1 or 0.
However, apart from the syntax error, the whole code seems pointless. In the end, you have a list of floors
times False
in a list.
So, you might as well:
blocked_floor = [False] * floors if floors > 0 else False # or, again, [False]
This is probably due to you not providing an example of the problem you’re actually trying to solve though.
You’re getting the syntax error because you need to finish the conditional and then move on to the iteration. You want to do the following:
blocked_floor = [False if floors > 0 else False for i in range(floors)]
Also, I don’t think you need the conditional since you’re just adding False no matter what the value of floors is. The following does the same thing but is simpler:
blocked_floor = [False for i in range(floors)]
I’m not quite sure what you’re trying to do with blocked_floor1.
I also found the following tutorial page helpful: https://riptutorial.com/python/example/767/conditional-list-comprehensions
I’m trying to add number of False in a list equivalent to a variable number, working if the variable is not 0. I’m trying to add a "else" statement if the variable is 0 but it’s not working.
Here is what I tired :
floors = 5
blocked_floor = [False for i in range(floors) if (floors > 0) else False]
blocked_floor1 = [False for i in range(floors) if (floors > 0) else False for i in range(1)]
There are a lot of topics about that issue but I tried everything that was in, nothing worked. I have a synthax error on the "else".
Do you have any idea about the issue ?
Your syntax is indeed wrong.
Instead of:
blocked_floor1 = [False for i in range(floors) if (floors > 0) else False for i in range(1)]
You wanted:
blocked_floor1 = [False for i in range(floors) if floors > 0] if floors > 0 else False
Or:
blocked_floor1 = [False for i in range(floors) if floors > 0] if floors > 0 else [False]
The difference being that in the first case, blocked_floor1
will be False
, and in the second case it will be [False]
. I’d think the first case is preferable, since otherwise you won’t be able to tall if floors was 1 or 0.
However, apart from the syntax error, the whole code seems pointless. In the end, you have a list of floors
times False
in a list.
So, you might as well:
blocked_floor = [False] * floors if floors > 0 else False # or, again, [False]
This is probably due to you not providing an example of the problem you’re actually trying to solve though.
You’re getting the syntax error because you need to finish the conditional and then move on to the iteration. You want to do the following:
blocked_floor = [False if floors > 0 else False for i in range(floors)]
Also, I don’t think you need the conditional since you’re just adding False no matter what the value of floors is. The following does the same thing but is simpler:
blocked_floor = [False for i in range(floors)]
I’m not quite sure what you’re trying to do with blocked_floor1.
I also found the following tutorial page helpful: https://riptutorial.com/python/example/767/conditional-list-comprehensions