Let d
be a large (but still fits into memory) Python dictionary where we do not know what the keys are. What is the most efficient way (efficient should mean something like the memory used to perform the task is small compared to the size of the dictionary and the speed should at least as fast any of the methods below) to get a key of d
(where it does not mater which key you get) and d
is unchanged either in content or order (for newer versions of Python) once you are done? This question is not about readability but about the python dictionary objects. For example two methods are:
- Use the
list
method
any_key = list(d)[0]
- Using the
popitem
method
any_key,y = d.popitem() d[any_key]=y
So both methods essentially implement a peekkey()
method. My basic timeit analysis shows that method 2) is must faster than method 1) and I assume that method 2) uses a lot less memory (but I do not really know if this true yet). Is method 2) “best” or is there something better?
Extra brownie points if you get a fast and a readable method using only Python. Even more points for a C/Python method that accesses the dictionary object directly if that method is significantly faster than the best python method.
Advertisement
Answer
If you do not care about which key you get, and you don’t mean “sample” in the random sense, then just grab the first key using next
key = next(iter(d.keys()))
which, for brevity, is the same as
key = next(iter(d))
Just to test performance, if I generate a dict with 1000 elements
d = {k:k for k in range(1000)}
then benchmarking these two methods, the next
approach is about 95% faster
>>> timeit.timeit('sample_key = list(d)[0]', setup='d = {k:k for k in range(1000)}') 5.3303698 >>> timeit.timeit('next(iter(d.keys()))', setup='d = {k:k for k in range(1000)}') 0.18915620000001354