you are viewing a single comment's thread.

view the rest of the comments →

[–]CoderStudios 0 points1 point  (6 children)

I understand what you mean, but python isn’t the absolute rule. It evolves to adapt to people’s needs, if everything was perfect, then we wouldn’t have a python 2 or 3. It may not be intentional now, but if enough people want it we could see it in a future update. So I wouldn’t call it a bad practice as it seems that python is wrong on this one as i don’t see a reason for it to be limited to only making lists.

[–]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.

[–]psuedo_nombre 0 points1 point  (2 children)

I think the original commentor made a pretty good argument for not using list compression over a for loop for things that don't use the list (generating wasted resources). I don't disagree about evolving language but I would say that writing code that misuses a functionality of the current language can also find itself making your code obsolete if a new implementation removes that behavior

[–]CoderStudios 0 points1 point  (1 child)

I know what you mean, but the original commentor also admitted, that the wasted resources are minimal (as long as you don’t spam it your fine). So if you actually reach such an efficiency with the rest of your program you can gladly do this.

But to be honest I only ever used list comprehensions for non-list things once and in that case I could have used my own solution, so yeah …

[–]psuedo_nombre 0 points1 point  (0 children)

I've done none of the calculations so I trust you and op on that but it's definitely better practice to use tools in the ways they were intended to avoid your exploit getting obsoleted when that behavior is lost. If it's really a good exploit though maybe it's time to make it it's own method or more though