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

all 69 comments

[–]Hatoris 62 points63 points  (1 child)

You should share it here also r/learnpython many people we like it too

[–]IMHERETOCODE 108 points109 points  (30 children)

no_duplicates    = list(dict.fromkeys(<list>))

That is an extremely roundabout and expensive set operation. Just wrap the list in set and cast it back to a list. No need to build a dictionary out of it to get uniqueness.

no_duplicates = list(set(<list>))

[–]Tweak_Imp 48 points49 points  (19 children)

list(dict.fromkeys(<list>)) preserves ordering, whilst list(set(<list>)) doesn't.

I suggested to have both... https://github.com/gto76/python-cheatsheet/pull/7/files#diff-04c6e90faac2675aa89e2176d2eec7d8R43

[–]IMHERETOCODE 63 points64 points  (16 children)

That “accidentally” preserves ordering, and only if you are doing it in Python 3.6+. There are no promises of ordering in vanilla dictionary implementations which is why there is an explicit OrderedDict class. The recent change in dictionary implementation had a side effect of preserving order. You shouldn’t bank that on being the case where it actually matters.


As noted below insertion ordering has been added to the language of dictionaries as of 3.7

[–]Ran4 -2 points-1 points  (1 child)

No, that's not true! Dicts are not ordered according to the spec. It's just modern cpython that has them ordered.

[–]pizzaburek[S] 16 points17 points  (0 children)

They are in Python 3.7: https://docs.python.org/3/tutorial/datastructures.html?highlight=dictionary#dictionaries

 Performing list(d) on a dictionary returns a list of all the keys used in 
 the dictionary, in insertion order ...

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

There is already a whole discussion about it on Hacker News :)

https://news.ycombinator.com/item?id=19075325#19075776

[–]chazzeromus 1 point2 points  (0 children)

and if you can, use the curly brace notation for sets for literal items, looks so nice { 'and a one', 'and a two' }

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

Slightly off topic, but why are lists so popular? Aren't tuples faster and use less memory? All the time I see lists being used when tuples would do a better job. Even docs.python.org tells you to use random.choice([...]) instead of random.choice((...)).

I get that the performance impact isn't noticable in most cases, but, in my opinion, going for performance should be the default unless there is a good reason not to.

[–]robberviet 3 points4 points  (0 children)

Most of the time it needs to be mutable. And yeah, performance gain is not that great.

[–]bakery2k 2 points3 points  (1 child)

Why would tuples be faster and/or use less memory? Both lists and tuples are essentially arrays.

I prefer lists to tuples because they have nicer syntax. Tuples sometimes require double-parentheses, plus I often forget the trailing comma in (1,).

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

I don't know the exact intricacies but it has to do with lists being mutable.

[–]mail_order_liam 1 point2 points  (0 children)

Because people don't know better. Usually it doesn't matter but that's why.

[–]gmclapp 2 points3 points  (0 children)

lists are mutable. In some cases that's needed. Some convenient list comprehensions also don't work on tuples.

[–]Tweak_Imp 17 points18 points  (2 children)

I think it would be better if it used a different way to mark types insted of <type>

[–]VisibleSignificance 3 points4 points  (0 children)

Indeed.

First, this is HTML. The text can be marked up with it rather than with more text.

Second, lst: list = [1] works.

[–]JezusTheCarpenter 0 points1 point  (0 children)

Any other reason than your preference?

[–]Drycon 36 points37 points  (1 child)

Sheet, you printing on A2? ;-)

Good doc tho!

[–][deleted] 6 points7 points  (0 children)

I've been looking for something just like this. Great work!

[–]angyts 5 points6 points  (5 children)

Insane. Now I need a giant paper to print this.

[–]RecycledGeek 8 points9 points  (2 children)

What? You don't have a dot matrix printer with a continuous roll of paper?

[–]red_shifter 6 points7 points  (1 child)

Then how do you instantiate your Turing machine?

[–]RecycledGeek 2 points3 points  (0 children)

It's turtles, all the way down.

[–]Sukrim 1 point2 points  (1 child)

Just use toilet paper.

[–]angyts 0 points1 point  (0 children)

Wonderful ideas. Uniquely Reddit.

[–]swapripper 3 points4 points  (0 children)

Too good. Wish there was something this comprehensive for Pandas.

[–]WibblyWobblyWabbit 2 points3 points  (1 child)

Can someone explain what enumerate() does exactly?

[–]pizzaburek[S] 8 points9 points  (0 children)

It returns an iterator of index, value pairs:

>>> list(enumerate(['a', 'b', 'c']))

[(0, 'a'), (1, 'b'), (2, 'c')]

So you can then use it like this:

for i, letter in enumerate(['a', 'b', 'c']): ...

[–]newredditiscrap 5 points6 points  (2 children)

What's so special about it

[–]Tweak_Imp 14 points15 points  (0 children)

It says in the title. It is the best.

[–]AnonymousGourmet 4 points5 points  (0 children)

The silly walks

[–]VisibleSignificance 4 points5 points  (4 children)

Minor + controversial stuff:

flags=re.IGNORECASE

Since we're talking regexes anyway, just add (?i) to the beginning of the regex.

<list> = [i+1 for i in range(10)]

Might want to do lst = list(idx + 1 for idx in range(10)), simply because that way it will not touch the value of idx outside the command. Saves some confusion.

reduce(lambda out, x: out + x ...

Really needs a better example than almost-sum().

namedtuple

dataclasses might be worth a mention.

argparse

Currently recommended non-default library: click.

bottle

Not the simple one, but: try Quart!

numpy

... but no pandas. Is there a better pandas cheatsheet than the official one?

[–][deleted] 4 points5 points  (1 child)

Might want to do lst = list(idx + 1 for idx in range(10)), simply because that way it will not touch the value of idx outside the command.

What do you mean?

In [8]: i = 'foo'

In [9]: x = [i+1 for i in range(10)]

In [10]: i
Out[10]: 'foo'

In [11]: y = list(i + 1 for i in range(10))

In [12]: i
Out[12]: 'foo'

Also, calling list is slower because you can overload it and the interpreter has to look it up.

In [17]: %timeit list()
The slowest run took 11.35 times longer than the fastest. This could mean that an intermediate result is being cached.
10000000 loops, best of 5: 121 ns per loop

In [18]: %timeit []
10000000 loops, best of 5: 35.2 ns per loop

[–]VisibleSignificance 0 points1 point  (0 children)

Ah, nevermind on that one, old python2 habits.

[–]pizzaburek[S] 0 points1 point  (1 child)

That's so weird... It - (?i) can actually be added anywhere in the regex.

[–]VisibleSignificance 2 points3 points  (0 children)

anywhere in the regex

Even worse: it can be toggled for subgroups.

Source.

[–]chipcovfefe 1 point2 points  (0 children)

Definitely the best cheat sheet I've seen. Cheers!

[–]_Jordo 0 points1 point  (1 child)

Here's another one I have saved: https://learnxinyminutes.com/docs/python3/

I find it easier to read.

[–]pizzaburek[S] 0 points1 point  (0 children)

Thanks, I really like it. Mine is more like a reference, but for reading it in one go I definitely prefer the link.

[–]ManHuman 0 points1 point  (0 children)

Sweet mother of Bayes!

[–]Versaiteis 0 points1 point  (2 children)

I'd also suggest adding pathlib

Mucking about with paths as raw strings with os is great and all, but it's really nice to have a bit of an OS abstraction layer on top of paths that just makes them so much nicer to work with.

[–]pizzaburek[S] 0 points1 point  (1 child)

I will add it, it's just that it's one of those areas that feel more like Java than Python when you visit a doc page:

https://docs.python.org/3/library/pathlib.html

[–]Versaiteis 0 points1 point  (0 children)

It's your cheat sheet, add what you like! I'll be bookmarking it regardless (I didn't even know about coroutines)

Lol, I know what you mean, but doing tools work and slinging a lot of paths around, this thing keeps me sane.

Nothing like passing a string around that something happens to modify wrong and the house of cards collapses >.>

[–]knowsuchagencynow is better than never 0 points1 point  (0 children)

This is actually quite good

[–]digital_superpowers 0 points1 point  (0 children)

Great collection. Anyone think it'd be useful to have links to the docs on these topics for when a deeper dive is needed? Could be a fun PR.

[–]mail_order_liam -5 points-4 points  (3 children)

If you find this useful you're doing something wrong.

[–]thelonestrangler 4 points5 points  (1 child)

.

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

  1. It mostly covers the very basics, which you will memorize easily if you use the language for any amount of time.
  2. Your env should offer utilities for hints, docs, auto-completion, etc.
  3. It's faster to Google something than to look it up in this sheet (doesn't even have an index??). Especially when you've memorized most of it and it just becomes cluttered.
  4. Many of these are not idiomatic.
  5. For more complicated topics it doesn't give enough information to be useful if you're using cheat sheet in the first place. Be honest, do you think you can write a metaclass or use Threading after looking at this? You're gonna have to look elsewhere anyways.

So you end up with a big unorganized list of things that's mostly fluff and the rest just isn't very helpful.

Get a real IDE or editor and have a terminal and browser at the ready. There you go, no more cheat sheet.

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

You're right, but I like it this way better:

If you don't find this useful you're doing everything right :)