Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: Dictionary that only retains last n inserted keys

I'm planning to read millions of small files from disk. To minimize i/o, I planned to use a dictionary that maps a file path to its content. I only want the dictionary to retain the last n keys inserted into it, though (so the dictionary will act as a cache).

Is there a data structure in Python that already implements this behavior? I wanted to check before reinventing the wheel.

like image 653
duhaime Avatar asked Jun 30 '18 14:06

duhaime


People also ask

How do you maintain the order of dictionary keys in Python?

Python's OrderedDict is a dict subclass that preserves the order in which key-value pairs, commonly known as items, are inserted into the dictionary. When you iterate over an OrderedDict object, items are traversed in the original order. If you update the value of an existing key, then the order remains unchanged.

Can a dictionary have empty keys?

In Python, we can use the zip() and len() methods to create an empty dictionary with keys. This method creates a dictionary of keys but returns no values from the dictionary.

Which dictionary method is used to remove the last inserted key value pair?

The popitem() method removes the item that was last inserted into the dictionary.

Which method is used to remove the last item from dictionary?

Explanation: Here, we are using the built-in popitem() method to remove the last element in the dictionary.


1 Answers

Use collections.deque for this with a maxlen of 6, so that it stores only the last 6 elements and store the information as key value pairs

from collections import deque
d = deque(maxlen=6)
d.extend([(1,1),(2,2),(3,3),(4,4), (5,5), (6,6)])
d
# deque([(1, 1), (2, 2), (3, 3), (4, 4), (5, 5), (6, 6)], maxlen=6)
d.extend([(7,7)])
d
# deque([(2, 2), (3, 3), (4, 4), (5, 5), (6, 6), (7, 7)], maxlen=6)
like image 103
Sunitha Avatar answered Sep 18 '22 23:09

Sunitha