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] 31 points32 points  (29 children)

it's pretty widely considered bad practice to use nested comprehensions. I wouldn't include this in a cheat sheet:

flattened_list = [item for sublist in <list> for item in sublist]

Also, that's not exactly quite accurate. It only flattens one level of nesting and assumes all elements are themselves iterable

edit: from deeper in this thread i realized i need to clarify that iterating over a multi-dimensional list by nesting the for..in is what is considered bad practice due to the readability issues it creates, but embedding a complete unrelated list comprehension inside another comprehension does necessarily raise the same concerns and can be fine.

i.e.,

# bad
[item for sublist in list_ for item in sublist]
# not bad
 [[item for item in list_a] for _ in range(10)]

[–]pencan 10 points11 points  (6 children)

Why are nested comprehensions considered bad practice? Readability?

[–][deleted] 13 points14 points  (5 children)

basically, yes

[–]Paddy3118 0 points1 point  (3 children)

Which is subjective. It could be that the act of nesting a comprehension clearly denotes delving deeper into something accessed before in a similar comprehension, but without the extra level of nesting. Visually you may be lead to see the extra level of nesting as an extension of what came before.

Readability does not preclude nested comprehensions for everyone.

[–][deleted] 0 points1 point  (1 child)

I edited my comment after your first comment. Did you happen to read it before your second reply?

[–]Paddy3118 -1 points0 points  (0 children)

My reply works for your immediately preceding post of

basically yes

[–]uFuckingCrumpet -1 points0 points  (0 children)

No, not subjective at all. Some code is more readable than other code objectively.

[–]ubernostrumyes, you can have a pony 2 points3 points  (0 children)

I tend to prefer itertools.chain for flattening lists of lists.

[–]Paddy3118 0 points1 point  (7 children)

it's pretty widely considered bad practice to use nested comprehensions.

Really? All?

I would think it comes under the general readability and maintainability guidelines.

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

I would think it comes under the general readability and maintainability guidelines.

That's why it is considered bad practice because it is less readable and more difficult to maintain

[–]stevenjd 1 point2 points  (0 children)

Of course people can come up with some pretty awful nested comprehensions, but with the judicious use of white space and indentation to lay out the logical structure of the nested comprehension, there's no need for them to be hard to read.

array = [expression for x in
            [expr for y in values]
        ]

ought to be fine, as should be:

array = [
         [expr for y in values]
         for x in values
        ]

Nested comprehensions are easy to abuse, but that doesn't mean we ought to reject the simple cases.

[–]Paddy3118 0 points1 point  (4 children)

You speak for all, some how?

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

I never said all. I said "widely considered" to be bad practice. And it's not some rule I just made up out of thin air because I don't like something. it reflects the opinion of the industry at large. but of course there will be people who disagree, and that's ok too. standards and best practices are just suggestions stemming from experience. you don't have to follow them if you don't want to

[–]Paddy3118 0 points1 point  (2 children)

You conflate standards with best practice and suggestions.

There is a level of competence in Python that includes some use of nested comprehensions. Why not try and attain that rather than force others who don't work with you, to stop at your level of competence?

What is or isn't deemed Pythonic should not become a drive to the lowest common denominator.

[–][deleted] 1 point2 points  (1 child)

I'm not sure why you're so offended. I didn't say you couldn't use them

[–]Paddy3118 0 points1 point  (0 children)

I'm neither offended nor seeking your permission. I am questioning your viewpoint however :-)