Nice, but why does this needs JS to be rendered ?
Comprehensive Python Cheatsheet (2018)
11–20 of 83 posts
Re: Comprehensive Python Cheatsheet (2018)
#12There 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]
Re: Comprehensive Python Cheatsheet (2018)
#13There 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]
Re: Comprehensive Python Cheatsheet (2018)
#14There 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]
[1] https://docs.python.org/2/tutorial/datastructures.html#dicti...
Re: Comprehensive Python Cheatsheet (2018)
#15There are several items that are dubious at best. Why would you use no_duplicates = list(dict.fromkeys( )) instead of no_duplicates = list(set( )) ?
Re: Comprehensive Python Cheatsheet (2018)
#16Earlier 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...
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)
#17Earlier 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.
Re: Comprehensive Python Cheatsheet (2018)
#18Earlier 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
Re: Comprehensive Python Cheatsheet (2018)
#19There are several items that are dubious at best. Why would you use no_duplicates = list(dict.fromkeys( )) instead of no_duplicates = list(set( )) ?
Re: Comprehensive Python Cheatsheet (2018)
#20Earlier 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.
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