Live data from Hacker News

Comprehensive Python Cheatsheet (2018)

gto76.github.io

11–20 of 83 posts

Re: Comprehensive Python Cheatsheet (2018)

#11
post #7

Nice, but why does this needs JS to be rendered ?

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.

Re: Comprehensive Python Cheatsheet (2018)

#12
post #8

There are several items that are dubious at best. Why would you use no_duplicates = list(dict.fromkeys( )) instead of no_duplicates = list(set( )) ?

>>> list(dict.fromkeys([3, 2, 1])) [3, 2, 1] >>> list(set([3, 2, 1])) [1, 2, 3]

Neither dict nor set preserve order. This is a misleading snippet.

Re: Comprehensive Python Cheatsheet (2018)

#13
post #8

There are several items that are dubious at best. Why would you use no_duplicates = list(dict.fromkeys( )) instead of no_duplicates = list(set( )) ?

>>> list(dict.fromkeys([3, 2, 1])) [3, 2, 1] >>> list(set([3, 2, 1])) [1, 2, 3]

I think this behavior will only be working starting from Python 3.6, as the dictionary obtained by calling `dict.fromkeys()` before this would not keep the ordering [1]

[1] https://youtu.be/p33CVV29OG8?t=489

Re: Comprehensive Python Cheatsheet (2018)

#14
post #8

There are several items that are dubious at best. Why would you use no_duplicates = list(dict.fromkeys( )) instead of no_duplicates = list(set( )) ?

>>> list(dict.fromkeys([3, 2, 1])) [3, 2, 1] >>> list(set([3, 2, 1])) [1, 2, 3]

I'm not sure what you're trying to imply. If it's regarding the order, you're wrong. The keys returned by either `keys()` or by `__iter__` are returned in arbitrary order [1].

[1] https://docs.python.org/2/tutorial/datastructures.html#dicti...

Re: Comprehensive Python Cheatsheet (2018)

#16
post #14

Earlier quoted context omitted.

>>> list(dict.fromkeys([3, 2, 1])) [3, 2, 1] >>> list(set([3, 2, 1])) [1, 2, 3]

I'm not sure what you're trying to imply. If it's regarding the order, you're wrong. The keys returned by either `keys()` or by `__iter__` are returned in arbitrary order [1]. [1] https://docs.python.org/2/tutorial/datastructures.html#dicti...

The whole cheatsheet is clearly in Python 3, where the same doc page that you linked states:

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

Re: Comprehensive Python Cheatsheet (2018)

#17

Earlier quoted context omitted.

>>> list(dict.fromkeys([3, 2, 1])) [3, 2, 1] >>> list(set([3, 2, 1])) [1, 2, 3]

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 Python sets, but it hasn't been done yet.

Re: Comprehensive Python Cheatsheet (2018)

#18

Earlier quoted context omitted.

>>> list(dict.fromkeys([3, 2, 1])) [3, 2, 1] >>> list(set([3, 2, 1])) [1, 2, 3]

I think this behavior will only be working starting from Python 3.6, as the dictionary obtained by calling `dict.fromkeys()` before this would not keep the ordering [1] [1] https://youtu.be/p33CVV29OG8?t=489

I love that talk. Raymond Hettinger is a gem. What I took from that is the implementation does preserve order but it isn't guaranteed yet and in theory it could change.

Re: Comprehensive Python Cheatsheet (2018)

#20

Earlier quoted context omitted.

I think this behavior will only be working starting from Python 3.6, as the dictionary obtained by calling `dict.fromkeys()` before this would not keep the ordering [1] [1] https://youtu.be/p33CVV29OG8?t=489

I love that talk. Raymond Hettinger is a gem. What I took from that is the implementation does preserve order but it isn't guaranteed yet and in theory it could change.

> I love that talk. Raymond Hettinger is a gem.

This talk is definitely worth a watch yes :)

> What I took from that is the implementation does preserve order but it isn't guaranteed yet and in theory it could change.

This was the case for python 3.6, but starting from Python 3.7 ordering is guaranteed [1]:

  Changed in version 3.7: Dictionary order is guaranteed to be insertion order.
[1] https://docs.python.org/3.7/library/stdtypes.html#dict-views
Post reply on HN