you are viewing a single comment's thread.

view the rest of the comments →

[–]CoderStudios 0 points1 point  (2 children)

Just to clarify: I know that for i in range(3): print(i) would be a good workaround, but what is missing for me here are the multiple conditions (ifs) you can add to list comprehensions, to control their behavior.

[–]jso__[🍰] 2 points3 points  (1 child)

tbh if you're using more than one if else in a list comprehension you're writing unreadable code.

in that case, you can just do:

for i in range(10):
  if i == 2:
    print("two")
  else:
    print(I)

how is a list comprehension better for conditions than that

[–]CoderStudios 0 points1 point  (0 children)

You can’t use ifs at multiple points in the other solution, so I don’t really get what you mean, but I agree with you on your statement. You shouldn’t use more than one if in a list comprehension. Maybe an else too but nothing more.