0

I want to use the one lined syntax for the "for_loop/if/elif/else/lists" for the code under:

for i in range(20):
    if(i<15):
        print("True")
    else:
        print("False")

this is a part of it

[True for i in range(20) if i<15]

How to add the "else" for it?

akram
  • 111
  • 7
  • 1
    The `[x for x in z if x]` is for filtering , `[x if x else y for x in z]` is for mapping each value regarding a condition – azro Nov 11 '21 at 13:08
  • 1
    try this : `[True if i<15 else False for i in range(20) ]` – I'mahdi Nov 11 '21 at 13:09

1 Answers1

1

Try it.

s = [True if i<15 else False for i in range(20)]
print(s)

output

[True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, False, False, False, False, False]
cnp
  • 339
  • 2
  • 11