• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / Python / Get Random Value from Dictionary in Python

Get Random Value from Dictionary in Python

May 19, 2022 Leave a Comment

To get a random value from a dictionary in Python, you can use the random module choice() function, list() function and dictionary values() function.

import random 

d = {"a":3, "b": 5, "c":1, "d":2}

print(random.choice(list(d.values())))

#Output:
5

If you want to get a random key from a dictionary, you can use the dictionary keys() function instead.

import random 

d = {"a":3, "b": 5, "c":1, "d":2}

print(random.choice(list(d.keys())))

#Output:
d

If you want to get a random key/value pair from a dictionary, you can use the dictionary items() function.

import random 

d = {"a":3, "b": 5, "c":1, "d":2}

print(random.choice(list(d.items())))

#Output:
('b',5)

When working with different collections of data, to be able to get a random piece of information from your data can be valuable.

In Python, many times we are working with dictionaries.

We can get a random value from a dictionary easily in Python using the random module choice() function – all we need to do is pass a list of the dictionary values.

To get the dictionary values, we can use the dictionary values() function and convert it to a list using list()

Below shows you an example of how to get a random value from a dictionary variable in Python.

import random 

d = {"a":3, "b": 5, "c":1, "d":2}

print(random.choice(list(d.values())))

#Output:
5

Get Random Key from Dictionary Using Python

If you want to get a random key from a dictionary, you can use the dictionary keys() function instead of the values() function.

Below shows you an example of how to get a random key from a dictionary variable in Python.

import random 

d = {"a":3, "b": 5, "c":1, "d":2}

print(random.choice(list(d.keys())))

#Output:
d

Get Random Key/Value Pair from Dictionary Using Python

If you want to get a random key/value pair from a dictionary, you can use the dictionary items() function.

Below shows you an example of how to get a random key/value pair from a dictionary variable in Python.

import random 

d = {"a":3, "b": 5, "c":1, "d":2}

print(random.choice(list(d.items())))

#Output:
('b',5)

Hopefully this article has been useful for you to learn how to get random values from a dictionary in Python.

Other Articles You'll Also Like:

  • 1.  Check if Number is Between Two Numbers Using Python
  • 2.  Check if Word is Palindrome Using Recursion with Python
  • 3.  How to Check If File is Empty with Python
  • 4.  Using Python to Add Items to Set
  • 5.  Python Check if List Index Exists Using Python len() Function
  • 6.  Using Python to Check if Number is Divisible by Another Number
  • 7.  Python tanh – Find Hyperbolic Tangent of Number Using math.tanh()
  • 8.  How to Measure Execution Time of Program in Python
  • 9.  How to Ask a Question in Python
  • 10.  Decrement For Loop with range() in Python

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy