News for hackers. Stuff that matters.
Re:Comprehensive Python Cheatsheet (2018) by omegote on Monday February 04, 2019 @07:10AM (#19075641)
There are several items that are dubious at best. Why would you use

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

    no_duplicates = list(set(<list>))

?
Re:Comprehensive Python Cheatsheet (2018) by pizzaburek on Monday February 04, 2019 @07:15AM (#19075660)

   >>> list(dict.fromkeys([3, 2, 1]))
   [3, 2, 1]
   >>> list(set([3, 2, 1]))
   [1, 2, 3]
Re:Comprehensive Python Cheatsheet (2018) by coldtea on Monday February 04, 2019 @08:59AM (#19076373)
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.

Re:Comprehensive Python Cheatsheet (2018) by snicker7 on Monday February 04, 2019 @09:08AM (#19076440)
As of 3.7 (3.6 for cpython), dictionaries have a deterministic order.
Re:Comprehensive Python Cheatsheet (2018) by thro_away_n on Monday February 04, 2019 @10:55AM (#19077294)
What's the best way to write code that relies on this deterministic order and prevent it from running incorrectly with older python versions?
Re:Comprehensive Python Cheatsheet (2018) by b_tterc_p on Monday February 04, 2019 @11:09AM (#19077443)
Import collections.OrderedDict as you would have done beforehand. There are still some differences between them.

(Dicts in 3.7 and ordered dicts)

https://stackoverflow.com/questions/50872498/will-ordereddic...

Re:Comprehensive Python Cheatsheet (2018) by BerislavLopac on Monday February 04, 2019 @09:32AM (#19076591)
If "the language explicitly says" something, then it's part of the specification and not an implementation detail.
Re:Comprehensive Python Cheatsheet (2018) by coldtea on Monday February 04, 2019 @09:37AM (#19076629)
Sure, hence the "unless" I've used.
Re:Comprehensive Python Cheatsheet (2018) by StreakyCobra on Monday February 04, 2019 @07:35AM (#19075772)
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) by bhrgunatha on Monday February 04, 2019 @07:49AM (#19075871)
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) by StreakyCobra on Monday February 04, 2019 @07:57AM (#19075921)
> 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
Re:Comprehensive Python Cheatsheet (2018) by teddyh on Monday February 04, 2019 @08:11AM (#19076008)
> Raymond Hettinger is a gem.

His talks are good, except that every time he asks a stupid rhetorical question of the audience I’m overwhelmed with the urge to wang¹ a tomato at him.

1. https://www.gunnerkrigg.com/?p=330

Re:Comprehensive Python Cheatsheet (2018) by misterdoubt on Monday February 04, 2019 @07:26AM (#19075723)
Neither dict nor set preserve order. This is a misleading snippet.
Re:Comprehensive Python Cheatsheet (2018) by woadwarrior01 on Monday February 04, 2019 @07:46AM (#19075857)
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) by BerislavLopac on Monday February 04, 2019 @09:33AM (#19076598)
More importantly, in Python 3.7 this exception has become the rule, i.e. it was introduced in the language specification
Re:Comprehensive Python Cheatsheet (2018) by dbcurtis on Monday February 04, 2019 @11:19AM (#19077527)
I am personally not a fan of details like this creeping into the language spec. CPython is not the only Python. This kind of spec creates unnecessary challenges for Micropython, for example. And of course there is Cython and others. It makes no sense to me that this should be in the language spec — you are essentially specifying a built in minefield of implementation bugs.
Re:Comprehensive Python Cheatsheet (2018) by joshuamorton on Monday February 04, 2019 @03:25PM (#19080263)
Cpython took this implementation detail from pypy (so it was already going to be the case in the two most used python implementations).

The reason being that the ordered dict ends up being faster than non-ordered, and people will rely on this implementation detail, so they added it to the spec to make that okay.

Re:Comprehensive Python Cheatsheet (2018) by misterdoubt on Monday February 04, 2019 @03:00PM (#19080069)
Well I'll be dipped.
Re:Comprehensive Python Cheatsheet (2018) by carlmr on Monday February 04, 2019 @07:17AM (#19075667)
Very cool!
Re:Comprehensive Python Cheatsheet (2018) by omegote on Monday February 04, 2019 @07:35AM (#19075776)
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) by pizzaburek on Monday February 04, 2019 @07:42AM (#19075826)
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) by detaro on Monday February 04, 2019 @08:08AM (#19075993)
only specified as such since Python 3.7, which hadn't been released when this cheat sheet was written, at least if the date at the top is accurate.

EDIT: clicking through to GitHub, the date is clearly not up-to-date, so this might have been added later.

Re:Comprehensive Python Cheatsheet (2018) by tyingq on Monday February 04, 2019 @07:52AM (#19075886)
They both choke on lists that contain lists.
Re:Comprehensive Python Cheatsheet (2018) by woadwarrior01 on Monday February 04, 2019 @07:39AM (#19075815)
list(dict.fromkeys(<list>)) preserves ordering, whilst list(set(<list>)) doesn't.