Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why don't dictionaries resize after deletions?

Apparently deleting entries in a dictionary doesn't trigger any resizes. A resize is triggered only after you add an entry.

This can be seen from the following:

# Drastic example, nobody does such 
# things with dicts FWIK
from sys import getsizeof

d = {i:i for i in range(100)}
print(getsizeof(d))  # 4704
for i in range(100):
    del d[i]  # similarly with pop
print(getsizeof(d))  # 4704
d[0] = 1 # triggers resize

as well as from a question on SO (from what I've found). sets behave in a similar fashion, which is to be expected to fall in line with what dicts do.

lists, on the other hand, resize when the the new size becomes half of that already allocated; this is stated in a list_resize comment:

/* Bypass realloc() when a previous overallocation is large enough
   to accommodate the newsize.  If the newsize falls lower than half
   the allocated size, then proceed with the realloc() to shrink the list.
*/

Why is it that dictionaries (and, indirectly, sets) don't employ a similar trick and instead wait for a new entry to be inserted? The behaviour described applies for Python 2.7 and 3.x (up until Python 3.7.0a0).

like image 361
Dimitris Fasarakis Hilliard Avatar asked Aug 02 '17 20:08

Dimitris Fasarakis Hilliard


People also ask

Are Python Dicts ordered?

As of Python version 3.7, dictionaries are ordered. In Python 3.6 and earlier, dictionaries are unordered. When we say that dictionaries are ordered, it means that the items have a defined order, and that order will not change.

What is the difference between delete and clear method in dictionary?

1 Answer. In Python dictionary, del keyword is used to delete a particular element. The clear( ) function is used to delete all the elements in a dictionary. To remove the dictionary, we can use del keyword with dictionary name.

Does dict maintain order?

Since dictionaries in Python 3.5 don't remember the order of their items, you don't know the order in the resulting ordered dictionary until the object is created. From this point on, the order is maintained. Since Python 3.6, functions retain the order of keyword arguments passed in a call.

Are dictionaries unordered?

Note that dictionaries are unordered, meaning that any time you loop through a dictionary, you will go through every key, but you are not guaranteed to get them in any particular order.


1 Answers

This is somewhat explained in Objects/dictnotes.txt, a companion file containing various notes on the dict implementation:

Dictionary operations involving only a single key can be O(1) unless resizing is possible. By checking for a resize only when the dictionary can grow (and may require resizing), other operations remain O(1), and the odds of resize thrashing or memory fragmentation are reduced. In particular, an algorithm that empties a dictionary by repeatedly invoking .pop will see no resizing, which might not be necessary at all because the dictionary is eventually discarded entirely.

One important consideration is that shrinking a list's buffer is really easy, while shrinking a dict's internal hash table is a much more complex operation.

like image 192
user2357112 supports Monica Avatar answered Sep 26 '22 08:09

user2357112 supports Monica