Python Hash Tables: understanding dictionaries
thepythoncorner.com
thepythoncorner.com
Of course it's arguable whether such a post belongs here, but a bunch of other introductory ones have been popular here before.
I haven't taken that Coursera course, but in college, I believe only calc I and II are the prereqs for intro to data structures and algorithms. Though I know solid engineers who understand applied CS who don't have a good grasp of math at all.
Me in freshman calculus: Why are we learning these Taylor series? It's so boring, and I'll never need to use it
Me three years later in upper-division meteorology: It turns out 90% of what we do is numerical methods because fluid dynamics is hard with limited data.
It's definitely easier for me to learn things when I have an application for it.
https://www.youtube.com/watch?v=0M_kIqhwbFo
I hate all algorithms courses except this one, really made me fall in love with algorithms and data structures .
If you want to go deeper, this is the next one on the topic
They are sorted. Normal hash tables are unsorted, sorting needs lot of time and space. Nobody but python would do that. (ignoring PHP here). But once you went down this path, users start relying on this quirks and you cannot get away from that.
They feature special values for ints. 1 hashes to 1, 4 to 4 and so on. This looks like Lua arrays, which are either arrays or hashtables under the hood, and allows efficient switches from dense to sparse arrays. But mostly it costs time in the most critical fast path. And it doesn't help in security at all.
They are insecure by default. Only with some special cmdline flag they feature a randomized seed. Which is still insecure because the seed can be easily exposed by reading the memory of the seed.
They are extremely slow.
They are using siphash. Everybody using siphash is immediately exposed as having no idea about hash table security.
They most definitely aren't sorted. It's not a quirk either. Python dicts are essentially a tiny hashtable of pointers into an array of objects. For iteration, that array is traversed directly. That's where the insertion-order preservation comes from. It's cheap. It doesn't involve sorting.
> They are insecure by default. Only with some special cmdline flag they feature a randomized seed.
Hashes are randomized since Python 3.2 or so.
> Which is still insecure because the seed can be easily exposed by reading the memory of the seed.
Obviously.
> They feature special values for ints. 1 hashes to 1, 4 to 4 and do on.
That's a quirk of the hash function (which is a reduction modulo a prime), not the hashtable.
> Everybody using siphash is immediately exposed as having no idea about hash table security.
Well...
No, they are insertion-ordered.
> Nobody but python would do that.
Ruby Hashes are insertion-ordered; JavaScript objects have numeric properties ordered numerically (so, in a sense, are sorted), but other propertied in insertion order.
```python linenums=”21” def get_value(self, input_key):
Also, might be worth noting that the "The Pythonic Implementation of Python Hash Tables" section seems to cover either Python 3.6 or 3.7, and that the implementation seems to have changed a bit in 3.8: now `sys.getsizeof({}) == 64` and `sys.getsizeof({"a": 100}) == 232`. Does anyone have the low-down on how it's changed and possibly how it's likely to change again in future versions?
It should be mentioned that a Python set type is basically a naked hash table, or a dictionary with all keys and no data.
As noted, in 3.6 dicts are ordered, but in 3.8 they can be reversed(); and in 3.9 the new merge and update operators are added:
https://docs.python.org/3.9/whatsnew/3.9.html#dictionary-mer...
Not exactly. Dicts, as you say, are ordered - but sets are not.
Python's set and dict are actually completely different codebases. Notably, the naturally ordered hash table of 3.6 dicts is not used by or for sets.
It was changed in 2005 for python 2.5 https://github.com/python/cpython/commit/9f1a6796eb83a2884df...
Not advocating for MD5, but it does strongly imply the use of cryptographic hash functions for hash tables, which is almost never the case, and a useful distinction to explain.
Cryptographic hash functions are trying to make the original data unrecoverable in any way, and essentially distributing as evenly throughout their output space as possible. They are also often trying to be slow to compute to prevent brute forcing.
On the other hand, hashing for a hash table doesn’t need to be secure in the same way (hence Python’s smaller bits just returning themselves as their hashed values). They also don’t need to distribute themselves evenly. They need to be fast, unlike a cryptographic hash, and they need to restrict the data to a fixed size.
Consider that I'm not a native speaker, so sometimes it's hard for me to send the exact message I would like to send... I will try to explain it better as soon as I will get anywhere I can use a computer :)
Thanks!
Moreover, I don't know anything about SEO or stuff like that so the ads are configured automatically (I went to the AdSense dashboard and clicked on the ”do whatever you want” button!!! :D )
However, I will try to limit the ads in the next few days understanding with the AdSense reports which one are not bringing earnings and can be removed.
Thanks for the feedbacks!
The article is a nice introduction, the rest of the website looks great and you don't seem to have included any of the modern stupid annoyances that will make me hate you (in-page popups, notification request, app install banners, chat bubbles).
Thanks for the article!
To learn about Python’s dictionaries, I’d recommend watching the talks by Brandon Rhodes and Raymond Hettinger.
* The Mighty Dictionary (PyCon 2010): https://www.youtube.com/watch?v=oMyy4Sm0uBs
* The Dictionary Even Mightier (PyCon 2017): https://www.youtube.com/watch?v=66P5FMkWoVU
There's also one by Raymond Hettinger, which seems to have been given first at a meetup and then, in shorter form, at PyCon 2017:
* Modern Dictionaries: https://www.youtube.com/watch?v=p33CVV29OG8
* Modern Python Dictionaries - A confluence of a dozen great ideas (PyCon 2017): https://www.youtube.com/watch?v=npw4s1QTmPg