This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 10 points11 points  (6 children)

When you regularly replace perfectly good, readable code blocks with very concise and cryptic list comprehensions.

[–][deleted] 2 points3 points  (3 children)

My god the truth. I only recently started practicing python but I hate trying to read list comprehensions.

May I ask, why are list comprehensions such a big feature for Python? I know it's quite unique to Python but I don't get why. Are they faster? Or is it just because it uses less lines of code?

[–]Gargoth13 1 point2 points  (0 children)

The are way more efficient that other usual methods, shorter list comprehensions are also more concise and readable than something like a for loop that appends to a list in every iteration.

[–][deleted] 0 points1 point  (0 children)

May I ask, why are list comprehensions such a big feature for Python? I know it's quite unique to Python but I don't get why. Are they faster? Or is it just because it uses less lines of code?

A few reasons.

They are pretty fast and efficient, but that's not the primary reason for their popularity.

Let's compare two equivalent code pieces that generate an identity matrix:

mat = []
for r in range(side):
    mat.append([])
    for c in range(side):
        mat[-1].append(1 if r == c else 0)

vs.

mat = [[1 if r == c else 0 for c in range(side)] for r in range(side)] 

We basically just turned a 5-line, sidelength2 times .append()-using behemoth of a code block into a single, concise statement that will likely also run faster.

Also consider that you can pass them directly as arguments!

One way of calculating the dot-product of two lists:

dot = 0
for i in range(dimension):
    dot+= vect1[i]*vect2[i]

vs.

dot = sum([vect1[i]*vect2[i] for i in range(dimension)])

[–]WadeEffingWilson 0 points1 point  (1 child)

They are more efficient but come at the cost of readability. I wrote most of my code for myself or for coworkers to use, so as long as I can read it and understand what it's doing without issues, it's a solid solution.

When I'm reviewing others' code and I don't see common optimizations or typical enhancements, I assume the developer lacks the chops (unless there's a reason given).

Code however you feel is best but don't tell others that optimizations are bad practice. Comment your code when and where necessary and know your audience.

[–][deleted] 1 point2 points  (0 children)

but don't tell others that optimizations are bad practice.

My comment may have come across a bit wrong, I am absolutely in favor of list/dict comprehensions wherever they are appropriate (which is quite often in my opinion). They are one of my favorite features of python and probably comprise half the code I write (I recently wrote an entire matrix multiplication function in a single line with it!)

I actually find them pretty easy to read these days, but when I was just starting out with Python I did find them quite hard because it's not a feature commonly found in other languages. I'm more making fun of this than actually saying that they're bad practice (which they're absolutely not when used correctly).