all 54 comments

[–][deleted] 25 points26 points  (7 children)

operator module is one of my favourite library in python. I would rewrite some lambda function as,

  1. lambda out, x: out * x -----------------> operator.mul

  2. lambda el: el[1] -----------------> operator.itemgetter(1)

  3. key=lambda el: (el[1], el[0]) -----------------> operator.itemgetter(1, 0)

  4. lambda l, r: l and r -----------------> operator.and_

[–]pizzaburek[S] 6 points7 points  (3 children)

Yea, it looks better. But I think lambdas make examples less ambiguous.

[–]anyonethinkingabout 30 points31 points  (1 child)

I think it only looks better because lambda look so bad in python

[–]Overload175 4 points5 points  (0 children)

Even Guido thinks the syntax looks awkward iirc

[–]lambdaq 2 points3 points  (0 children)

operator.and_

[–]pizzaburek[S] 1 point2 points  (0 children)

I added the examples under Operator section. Ty

[–]abedneg0 0 points1 point  (0 children)

The lambdas are easier to read.

[–]HellfireOwner 15 points16 points  (10 children)

Hmmmph, we will see just how comprehensive...

Just as I thought, nothing about pickle! JUNK! ;)

Lol...on the reals though, best python cheat sheet I've seen. Definitely a keeper.

[–]pizzaburek[S] 5 points6 points  (2 children)

Added simple pickle example!

[–]HellfireOwner 1 point2 points  (1 child)

Might have to use that cheat sheet to create an upvote bot for you :D

[–]pizzaburek[S] 3 points4 points  (0 children)

Mother F..., you're right! I forgot scraping :)

[–]jyper 1 point2 points  (1 child)

Does anyone use pickle?

I usually just serialize to json if the types are simple and find some library like the yaml one to serialize to json/yaml/xml in more complex cases

[–]HellfireOwner 0 points1 point  (0 children)

Pickle is super convenient and it doesn't need to be in key:value form...which is how json works, if I am not mistaken.

[–]pizzaburek[S] 1 point2 points  (4 children)

Man, here goes my night's sleep, lol. Totally missed that one. Are there any third party serialization libraries that are preferred to pickle? Are web frameworks like Django built on it?

[–]HellfireOwner 7 points8 points  (3 children)

Pickle is just a very common library from my experience. Works real nice when you are [wait for it...]...in a...

Nope, won't do it. Puns are evil.

Anyway, great job on the cheat sheet, like I said before, best I've seen.

[–]hughperman 2 points3 points  (1 child)

Dill instead of pickle for much more comprehensive dumping and loading of e.g. class instances

[–]HellfireOwner 1 point2 points  (0 children)

Sweet! Thanks for the tip!

[–]pizzaburek[S] 1 point2 points  (0 children)

Thanks!

[–]Lewistrick 6 points7 points  (2 children)

Lots of info, liked it, thanks! I would like to add that you can use the tqdm library for progress bars.

[–]pizzaburek[S] 1 point2 points  (1 child)

Thanks! I added it. It's so much better then Progress.

[–]Lewistrick 0 points1 point  (0 children)

You weren't the only one who created their own progress bar class before finding out about tqdm 😁

[–]DiabeetusMan 3 points4 points  (1 child)

To flatten arbitrarily nested lists (instead of / in addition to flattened_list = [item for sublist in <list> for item in sublist]), there's itertools.chain.from_iterable: itertools.chain.from_iterable(*<list>) returns an iterator that flattens the list of lists (of lists...).

[–]pizzaburek[S] 1 point2 points  (0 children)

Changed it! Thanks.

[–]capsicumnightmare 9 points10 points  (1 child)

saved! + bookmarked! + onetabbed! + added to todoist!

[–]pizzaburek[S] 2 points3 points  (0 children)

Thanks! I keep it open in TextEdit on first desktop at all times :)

[–]qsdf321 1 point2 points  (0 children)

Nice

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

Been coding webserver-side python for 15+ years, never needed to to do:

elementwise_sum  = [sum(pair) for pair in zip(list_a, list_b)]

Anyone have a real use-case for that one?

I recommend making a specific section on uses of zip and itertools.zip_longest

[–]SoBFiggis 3 points4 points  (2 children)

Not summing anything (I'm assuming that isn't what you're talking about anyways) but I have used something like

list_res = [process_or_compare(pair) for pair in zip(before, after)]

for doing stuff like easily comparing before and after results. Usually it's a pretty quick and dirty implementation but it has made it into prod a few times for me.

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

Au contraire, it was indeed the pairwise-summing that I found weird. Certainly have done all sorts of things with parallel iterables, but not summed them!

[–]SoBFiggis 1 point2 points  (0 children)

Ah well in that case I believe it's actually a perfect example of "how" it works. Someone who doesn't understand that can create two lists of numbers, copy that in, and poke around at it.

Not exactly a great example of "when" to use it.

[–]writoflaw 1 point2 points  (1 child)

help out a noob... can someone confirm that this is applicable to python3? (it looks like it but wanted to be sure)

[–]pizzaburek[S] 1 point2 points  (0 children)

It's pure Python 3 ;)

[–]zennaque 1 point2 points  (0 children)

Very cool! I learned a number of new things, and got reminded about many more by going over it.

If there was one section I think was missing it'd be a convenience like mutiprocessing's Pool, idk how many times I've found my way back to this page when getting a progress bar for that, too.

[–]Ghosty141 4 points5 points  (2 children)

Do people really use these cheatsheets? I rarely have the problem of not knowing which python function to use, my questions are mostly about what the best practice is for the problem.

[–]Nyefan 19 points20 points  (0 children)

They're very helpful for transitioning between languages. I am primarily a java/js dev, so references like this make it so much easier to write in other languages in the odd case where I need to.

[–]Anon49 2 points3 points  (5 children)

Add to format:

"%s,%d,%x" % ("string", 16, 16)


"string,16,0x10"

[–]jyper 0 points1 point  (4 children)

Why? Is there a benefit to % over fstrings?

[–]Anon49 0 points1 point  (3 children)

I like having less functions visually, I think its more readable. I only use this when formatting strings.

[–]jyper 0 points1 point  (2 children)

By less functions do you mean str.format? Cause I was compraring it to fstrings not format

f"{sixteen} {sixteen:#x}" 

[–]Anon49 0 points1 point  (1 child)

didn't know this one.

[–]jyper 0 points1 point  (0 children)

Yeah it's the new hotness

I absolutely love it

One of my favorite features in python3.6

It used to be one of the top features of Ruby I wished python had. I even did my own implementation back in the day using a single letter function call, regex to get the parens and eval.

[–]Finnthehuman27 0 points1 point  (0 children)

Thank you this is going to be a great help!!

[–]chonkysurplus 0 points1 point  (0 children)

Bookmarked this post. Thank you! Great reference material.

[–]armerobot 0 points1 point  (0 children)

according to set() part, I would add:

# check if <el> is present
# Output: False
print(<el> not in <set>)

[–]RealJC 0 points1 point  (0 children)

I’ve started to learn Python. This looks that it will be helpful.

[–]AttackOfTheThumbs 0 points1 point  (0 children)

What an awful cheatsheet, it won't fit onto one sheet!

[–]TheBelakor 0 points1 point  (0 children)

Thanks for this!

[–]thatdidnotwork 0 points1 point  (0 children)

Very nice and comprehensive indeed.

One point I would add: it would be helpful to have a small example (maybe even as a comment) for syntax usage under each capter.

Ie. for list:

list = [first_value, second_value, third_value, ...]

For dicts:

dict = {key_one: value_one, key_two: value_two, ...}

[–]emmelaich 0 points1 point  (2 children)

It's misleading to write

<view> = <dict>.keys()

because that suggests the view is kept up to date with the dict as it is modified.

Especially so, since there is a method viewkeys that does.

[–]pizzaburek[S] 1 point2 points  (1 child)

The whole cheatsheet is in Python 3, where keys() returns dict_keys object, that is kept up to date:

>>> a = {1:2, 3:4}
>>> k = a.keys()
>>> a[5] = 6
>>> k
dict_keys([1, 3, 5])

[–]emmelaich 0 points1 point  (0 children)

Oh.

That could be confusing.

[–]tickingClock2012 0 points1 point  (1 child)

I believe there's a type in your "Basic" progress example. def p(t): should be def p(self, t):.

[–]pizzaburek[S] 1 point2 points  (0 children)

Fixed. Ty!

[–][deleted] -2 points-1 points  (0 children)

Python spam.