How can I get a random key-value pair from a dictionary?
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
In Python, dictionaries are optimized for key lookup, not random indexing. That means there is no direct built-in method to ask a dictionary for "one random item," so the usual solution is to pick from a temporary list or pick a random key and then retrieve its value.
The Simplest Correct Approach
For ordinary code, convert the dictionary items to a list and use random.choice.
This is clear and correct. Each key-value pair has equal probability because list(data.items()) produces one entry per dictionary item.
Picking a Random Key First
If you only want a random item and do not mind two steps, you can choose a random key and then index back into the dictionary.
This is functionally equivalent for normal dictionaries because iterating a dictionary yields keys.
Handling Large Dictionaries
The downside of both common patterns is that they build a temporary list. For a small or medium dictionary, that cost is negligible. For a very large dictionary, repeated random selection can become expensive because you keep materializing the keys or items.
If you need many random selections, consider caching the keys or items once and keeping them in sync with the dictionary.
This is especially useful when the dictionary is mostly read-only and the selection happens often.
What About random.sample?
random.sample also works if you want one or more random keys, but for a single choice it is less direct than random.choice.
Use sample when the real problem is "pick several distinct keys" rather than "pick one random pair."
Empty Dictionaries Need a Guard
An empty dictionary has no random item to choose. Both random.choice and random.sample will raise an exception if you pass an empty sequence.
Guarding explicitly makes the failure mode clearer to callers.
If You Need Cryptographic Randomness
The random module is fine for simulations, shuffling, games, and ordinary application logic. It is not intended for security-sensitive selection.
If the choice affects tokens, secrets, or security decisions, use secrets.choice instead:
The surrounding pattern stays the same; only the randomness source changes.
Why There Is No dict.random_item()
Python dictionaries preserve insertion order, but they are still hash tables under the hood, not array-like containers with cheap random indexing. Adding a built-in random item operation would not avoid the basic issue that keys are not stored as a simple indexable list for API purposes.
That is why explicit conversion is the idiomatic approach.
Common Pitfalls
The most common mistake is trying random.choice(data) directly. A dictionary is iterable, but it is not a sequence, so choice cannot index it the way it can index a list.
Another mistake is rebuilding list(data.items()) inside a hot loop when the dictionary rarely changes. Cache the list if you need repeated random picks.
Developers also forget to handle the empty-dictionary case, which leads to an unhelpful exception from deeper inside the random module.
Finally, be clear about whether you need a random key, a random value, or a random key-value pair. Those are related but slightly different tasks.
Summary
- The simplest solution is
random.choice(list(my_dict.items())). - You can also pick a random key and then read the corresponding value.
- For repeated selection, cache keys or items instead of rebuilding lists every time.
- Guard against empty dictionaries before choosing.
- Use
secrets.choiceinstead ofrandom.choicefor security-sensitive selection.
Related reading
- How can i get all group list with kafka0.10.x
- How can I get dictionary key as variable directly in Python not by searching from value?
- How can I get key's value from dictionary in Swift?
- How can I get list of values from dict?
- How can I get a value from a cell of a dataframe?
- How can I get current CPU and RAM usage in Python?
- How can I get the concatenation of two lists in Python without modifying either one?
- How can I get the current stack trace in Java?

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.