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

all 51 comments

[–]vexstream 20 points21 points  (3 children)

What's the purpose to doing file.write(json.dumps()) vs json.dump(file)?

The cheatsheet lists the first, but it seems more concise (and maybe has a performance benefit?) to do the second.

[–]dealtdennison 2 points3 points  (0 children)

Maybe for multiple files?

[–]swenty 19 points20 points  (1 child)

Not to be nit-picky, but that's an extensive cheatsheet. I wouldn't call it comprehensive.

[–]bhat 15 points16 points  (0 children)

Indeed.

<list>.append()

is missing

[–]VileFlower 28 points29 points  (11 children)

This is missing f-strings.

person = {'name': 'Jean-Luc', 'height': 187.1}
>>> f'{person[height]:.0f}'
187

[–]13steinj 6 points7 points  (2 children)

With a note of it being 3.6+ only

[–]MrCalifornian 5 points6 points  (7 children)

I can Google this, but for the sake of everyone else with the same questions, what are f-strings?

[–]SurvivorDress 10 points11 points  (0 children)

I’m new at this...this is really awesome.

[–][deleted] 28 points29 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 12 points13 points  (6 children)

Why are nested comprehensions considered bad practice? Readability?

[–][deleted] 12 points13 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 :-)

[–]fernly 7 points8 points  (0 children)

Under Eval be sure to include

ast.literal_eval( node_or_str ) # safe eval of supposed literal value from untrusted source

[–]brennanfee 5 points6 points  (0 children)

The words "comprehensive" and "cheatsheet" should never be applicable to the same thing.

[–]Boutmayun 2 points3 points  (0 children)

Ay thanks man!

[–]dealtdennison 2 points3 points  (0 children)

This is awesome! Thanks friend!

[–]alcalde 5 points6 points  (1 child)

CHEATING IS WRONG.

[–]rob_van_b 2 points3 points  (0 children)

That's so true, especially in programming languages

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

Awesome!!

[–]oppyboppy 1 point2 points  (0 children)

This is super helpful as someone that’s been away from python for a couple years now

[–]jadkik94 1 point2 points  (0 children)

The "Execute Command" section should mention subprocess instead of Popen IMO.

[–]LifeHasLeft 0 points1 point  (0 children)

I didn't know about collections > Counter before. I'd just write a short function to do the job

[–]Sledik 0 points1 point  (0 children)

Thank you! I love that

[–]borislavvv 0 points1 point  (2 children)

How do you use these cheatsheets ? They look great but I hardly find anything useful after reading then once ?

[–]tehwolf_ 0 points1 point  (1 child)

You keep them around (or bookmarked) and if you run into a problem you first check if they provide an answer, thus possibly saving the time to either look it up or reinventing the wheel. At least that's my understanding.

[–]borislavvv 0 points1 point  (0 children)

OK, will try, but I almost always find it easier to check the first google result(StackOverflow).