Earlier quoted context omitted.
>>> list(dict.fromkeys([3, 2, 1])) [3, 2, 1] >>> list(set([3, 2, 1])) [1, 2, 3]
Dictionaries should not be assumed to keep their orderings anyway. That's why there's OrderredDict. Unless the language explicitly says that dicts hold their order, even if they do, it's an implementation detail, and it should not be relied upon.
Comprehensive Python Cheatsheet (2018)
41–50 of 83 posts
Re: Comprehensive Python Cheatsheet (2018)
#42Earlier quoted context omitted.
Neither dict nor set preserve order. This is a misleading snippet.
In Python 3.6+, dictionaries preserve insertion order. This is done by storing the keys, values (and cached hash values) in a separate array, and the hashtable is a succinct array of indexes into this array. This results in more compact dictionaries, which are also a bit faster because of it's cache friendliness. Preserving insertion order is a happy side effect of this. This optimization can also be applied to Pytho…
Re: Comprehensive Python Cheatsheet (2018)
#43Earlier quoted context omitted.
Dictionaries should not be assumed to keep their orderings anyway. That's why there's OrderredDict. Unless the language explicitly says that dicts hold their order, even if they do, it's an implementation detail, and it should not be relied upon.
If "the language explicitly says" something, then it's part of the specification and not an implementation detail.
Re: Comprehensive Python Cheatsheet (2018)
#44Earlier quoted context omitted.
Because it's rendered directly from README.md. That way it's easier for me, because I don't have to render it every time I make a change (I make a lot of little edits all the time), and project's Github page ( https://github.com/gto76/python-cheatsheet ) always has the same content as webpage.
It's quite annoying to have to enable JS for 3 different domains just to see something other than a blank page. It also prevents non-JS browsers and retrievers from seeing content. If you were to use e.g. org-mode for the source document, you could easily export to HTML automatically when the file is saved. You could also easily use a git hook to run e.g. pandoc to convert md to HTML automatically. There are many way…
Re: Comprehensive Python Cheatsheet (2018)
#45Re: Comprehensive Python Cheatsheet (2018)
#46Can we please change the current clickbait title (Best Python Cheatsheet Ever!) to the original one (Comprehensive Python Cheatsheet)?
Re: Comprehensive Python Cheatsheet (2018)
#47Earlier quoted context omitted.
I would put it after R help function and, even better, F1 key to get the full documentation of any thing in Rstudio.
I'm amazed you think the R documentation is anything but terrible. Compare the R vector page: https://stat.ethz.ch/R-manual/R-devel/library/base/html/vect... To the equivalent python documentation: https://docs.python.org/3/tutorial/datastructures.html
Re: Comprehensive Python Cheatsheet (2018)
#48I like the clean look and the overall way it is presented. The examples using angle brackets (eg.: .append( ) ) is highly legible and the one column format is faster to scan.
I came here to say exactly the opposite. The angle brackets mean that every single example is a syntax error. If you are going to write a cheatsheet with samples of Python code, why not write it in valid Python? This is clearly aimed at beginners as the samples are or very basic things, yet this is going to be confusing to them as they will think that ` ` is syntax. Have a look at the Python docs for much better ways…
The Python docs (example here[1]) on the other hand limit themselves to {}, [] and () to differentiate between dicts, lists and tuples, but doesn't really differentiate between these and the more generalized notions of collections/iterators/element, etc.
As someone who codes in statically typed languages and has an intuitive sense of upcasting / downcasting / contravariance / covariance, this level of precision makes sense to me and enhances my appreciation for Python types.
That said, I've also been writing Python professionally since 2005 so I'm comfortable with not worrying too much about types in Python -- with dynamic typing things just work as long as they have the right shape and behavior.
[1] https://docs.python.org/3/library/threading.html#threading.T...
Re: Comprehensive Python Cheatsheet (2018)
#49Empty set literal: {*()}
MIND = BLOWNRe: Comprehensive Python Cheatsheet (2018)
#50Earlier quoted context omitted.
Dictionaries should not be assumed to keep their orderings anyway. That's why there's OrderredDict. Unless the language explicitly says that dicts hold their order, even if they do, it's an implementation detail, and it should not be relied upon.
As of 3.7 (3.6 for cpython), dictionaries have a deterministic order.