Depending on your needs, you can use a list to store your values. This will only use about 16% of the space that the dictionary uses, but some operations, such as searching and inserting, will be (possibly a lot) slower.
values = list(range(56000))
If you use the bisect module and save your values ββin a sorted list, your searches will still be slower than using a dict, but much faster than the naive x in my_list .
The list should always be stored in sorted order. To check if there is a value in your list, you can use this function:
def is_in_list(values, x): i = bisect_left(values, x) return i != len(values) and values[i] == x
It works as follows:
>>> is_in_list([2, 4, 14, 15], 4) True >>> is_in_list([2, 4, 14, 15], 1) False >>> is_in_list([2, 4, 14, 15], 13) False
This method will significantly reduce memory usage, but - compared to dict or set - the search requires O (log n), not O (1), and the insert takes O (n) instead of O (1).
source share