-1

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 ?

Geoffrey
  • 27
  • 4
  • So if `floors <= 0` you want a list with one instance of `False` in it, otherwise you want a list with one instance of `False` for each floor? – The Photon Jan 15 '23 at 00:31
  • Why not `[False for i in range(floors)] if floors > 0 else [False]`? (use with caution if you're going to modify the list later) – The Photon Jan 15 '23 at 00:32
  • That's exactly that, I want one instance of ```False``` if ```floors <= 0```, but if possible in a complete list comprehension because I need to modify some False with True later. Is it possible ? – Geoffrey Jan 15 '23 at 00:36
  • https://stackoverflow.com/questions/4260280/if-else-in-a-list-comprehension does this answer your question? you have to put the if else before the for – zaki98 Jan 15 '23 at 00:36
  • Just use `[False][:]` or one of the other ways of cloning the list if you need to modify the list later. – The Photon Jan 15 '23 at 00:40
  • Or skip the list comprehension and just use `[False] * max(1, floors)`. – The Photon Jan 15 '23 at 00:41
  • `blocked_floor = [False for i in range(floors) if (floors > 0) else False]` This is _always_ gonna be False... did you mean `... else True`? – John Gordon Jan 15 '23 at 00:44

2 Answers2

1

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.

Grismar
  • 27,561
  • 4
  • 31
  • 54
0

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

  • 1
    If `floors` is 0, `range(floors)` will be empty, and you'll get an empty list instead of the list `[False]` that OP wants. – The Photon Jan 15 '23 at 00:43