-5

Often you see question asked about a better method of doing something, or just generally a looping question and very often the top answers will use some form of convoluted list/dict/tuple comprehension that takes longer for others to understand than create themselves. While a simple and understandable loop could have just been made.

Since it cannot provide any speed benefits that I could imagine, is there any use of it in python other than to look smart or be Pythonic?

Thanks.

Chrismon Chin
  • 429
  • 2
  • 12
  • 2
    This is a rant, not a question. – Jared Smith Aug 28 '19 at 15:14
  • "Simple" and "understandable" are somewhat subjective, but be aware that there are people that *greatly* overuse (and abuse) list comprehensions. – chepner Aug 28 '19 at 15:14
  • @JaredSmith It is a question on whether there are reasons for its use that I have not taken into account. To decide whether I should adopt it more. A rant would not ask for input that could drastically change ones not yet fully formed opinion, but instead try to persuade others towards ones opinion. Maybe it is just a poorly worded question. But i have to include my current opinions to better direct the answers. – Chrismon Chin Aug 28 '19 at 15:18
  • 1
    @ChrismonChin non-ranty version of your question: "I notice in other answers here on SO that often a comprehension is used where a loop would arguably be clearer, examples: [list of links to examples]. Are there any *objective* advantages to the comprehension over a loop in these cases?". – Jared Smith Aug 28 '19 at 15:22
  • 1
    @JaredSmith Thank you – Chrismon Chin Aug 28 '19 at 15:24

1 Answers1

0

I believe the goal in this case to make your code as concise and efficient as possible. At times it can seem convoluted, but the computer looping through multiple lines as opposed to a single line adds processing time, which in large applications and across many iterations can cause some delays.

Additionally, although it seems harder to understand initially, for an outside individual reading your code, it's much quicker for them to read simplified expressions than pages of loops to get an idea of what you're attempting to accomplish.

  • I would assume the compiler would process the list comprehension no different than a similar list, but I could be wrong. Thank you. – Chrismon Chin Aug 28 '19 at 15:23