{"id":128068,"date":"2022-09-13T16:34:28","date_gmt":"2022-09-13T16:34:28","guid":{"rendered":"https:\/\/blog.finxter.com\/?p=669667"},"modified":"2022-09-13T16:34:28","modified_gmt":"2022-09-13T16:34:28","slug":"how-to-get-a-random-entry-from-a-python-dictionary","status":"publish","type":"post","link":"https:\/\/sickgaming.net\/blog\/2022\/09\/13\/how-to-get-a-random-entry-from-a-python-dictionary\/","title":{"rendered":"How to Get a Random Entry from a Python Dictionary"},"content":{"rendered":"\n<div class=\"kk-star-ratings kksr-auto kksr-align-left kksr-valign-top\" data-payload=\"{&quot;align&quot;:&quot;left&quot;,&quot;id&quot;:&quot;669667&quot;,&quot;slug&quot;:&quot;default&quot;,&quot;valign&quot;:&quot;top&quot;,&quot;ignore&quot;:&quot;&quot;,&quot;reference&quot;:&quot;auto&quot;,&quot;class&quot;:&quot;&quot;,&quot;count&quot;:&quot;1&quot;,&quot;readonly&quot;:&quot;&quot;,&quot;score&quot;:&quot;5&quot;,&quot;best&quot;:&quot;5&quot;,&quot;gap&quot;:&quot;5&quot;,&quot;greet&quot;:&quot;Rate this post&quot;,&quot;legend&quot;:&quot;5\\\/5 - (1 vote)&quot;,&quot;size&quot;:&quot;24&quot;,&quot;width&quot;:&quot;142.5&quot;,&quot;_legend&quot;:&quot;{score}\\\/{best} - ({count} {votes})&quot;,&quot;font_factor&quot;:&quot;1.25&quot;}\">\n<div class=\"kksr-stars\">\n<div class=\"kksr-stars-inactive\">\n<div class=\"kksr-star\" data-star=\"1\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" data-star=\"2\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" data-star=\"3\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" data-star=\"4\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" data-star=\"5\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div class=\"kksr-stars-active\" style=\"width: 142.5px;\">\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/div>\n<div class=\"kksr-legend\" style=\"font-size: 19.2px;\"> 5\/5 &#8211; (1 vote) <\/div>\n<\/div>\n<h2 class=\"wp-embed-aspect-16-9 wp-has-aspect-ratio\">Problem Formulation and Solution Overview<\/h2>\n<p class=\"wp-embed-aspect-16-9 wp-has-aspect-ratio\">This article will show you how to get a random entry from a Dictionary in Python.<\/p>\n<p>To make it more interesting, we have the following running scenario:<\/p>\n<p class=\"wp-embed-aspect-16-9 wp-has-aspect-ratio has-base-2-background-color has-background\"><img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f468-200d-1f3eb.png\" alt=\"\ud83d\udc68\u200d\ud83c\udfeb\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/> <strong>The Plot<\/strong>: <em>Mr. Sinclair, an 8th great Science Teacher, is giving his students a quiz on the first 25 <em><a rel=\"noreferrer noopener\" href=\"https:\/\/ptable.com\/?lang=en#Properties\" target=\"_blank\">Periodic Table<\/a><\/em> elements. He has asked you to write a Python script so that when run, it generates a random key, value, or key:value pair from the Dictionary shown below to ask his students.<\/em><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">els = {'Hydrogen': 'H', 'Helium': 'He', 'Lithium': 'Li', 'Beryllium': 'Be', 'Boron': 'B', 'Carbon': 'C', 'Nitrogen': 'N', 'Oxygen': 'O', 'Fluorine': 'F', 'Neon': 'Ne', 'Sodium': 'Na', 'Magnesium': 'Mg', 'Aluminum': 'Al', 'Silicon': 'Si', 'Phosphorus': 'P', 'Sulfur': 'S', 'Chlorine': 'Cl', 'Argon': 'Ar', 'Potassium': 'K', 'Calcium': 'Ca', 'Scandium': 'Sc', 'Titanium': 'Ti', 'Vanadium': 'V', 'Chromium': 'Cr', 'Manganese': 'Mn'}<\/pre>\n<hr class=\"wp-block-separator has-alpha-channel-opacity\" \/>\n<p class=\"wp-embed-aspect-16-9 wp-has-aspect-ratio has-global-color-8-background-color has-background\"><img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f4ac.png\" alt=\"\ud83d\udcac\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/> <strong>Question<\/strong>: How would we write code to get a random entry from a Dictionary<em>?<\/em><\/p>\n<p class=\"wp-embed-aspect-16-9 wp-has-aspect-ratio\">We can accomplish this task by one of the following options:<\/p>\n<ul type=\"video\" class=\"wp-embed-aspect-16-9 wp-has-aspect-ratio\">\n<li><strong>Method 1<\/strong>: Use <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/python-random-module\/\" data-type=\"URL\" data-id=\"https:\/\/blog.finxter.com\/python-random-module\/\" target=\"_blank\"><code>random.choice()<\/code><\/a> and <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/python-dict-items-method\/\" data-type=\"URL\" data-id=\"https:\/\/blog.finxter.com\/python-dict-items-method\/\" target=\"_blank\"><code>items()<\/code><\/a><\/li>\n<li><strong>Method 2<\/strong>: Use <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/python-random-module\/\" data-type=\"URL\" data-id=\"https:\/\/blog.finxter.com\/python-random-module\/\" target=\"_blank\"><code>random.choice()<\/code><\/a> and <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/python-dict-keys-method\/\" data-type=\"URL\" data-id=\"https:\/\/blog.finxter.com\/python-dict-keys-method\/\" target=\"_blank\"><code>keys()<\/code><\/a><\/li>\n<li><strong>Method 3<\/strong>: Use <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/python-random-module\/\" data-type=\"URL\" data-id=\"https:\/\/blog.finxter.com\/python-random-module\/\" target=\"_blank\"><code>random.choice()<\/code><\/a><a href=\"https:\/\/blog.finxter.com\/python-random-module\/\"> <\/a>and <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/python-dictionary-values-method\/\" data-type=\"URL\" data-id=\"https:\/\/blog.finxter.com\/python-dictionary-values-method\/\" target=\"_blank\"><code>values()<\/code><\/a><\/li>\n<li><strong>Method 4<\/strong>: Use <a rel=\"noreferrer noopener\" href=\"https:\/\/docs.python.org\/3\/library\/random.html\" data-type=\"URL\" data-id=\"https:\/\/docs.python.org\/3\/library\/random.html\" target=\"_blank\"><code>sample()<\/code><\/a><\/li>\n<li><strong>Method 5<\/strong>: Use <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/python-random-seed-a-deep-dive\/\" data-type=\"URL\" data-id=\"https:\/\/blog.finxter.com\/python-random-seed-a-deep-dive\/\" target=\"_blank\"><code>np.random.choice()<\/code><\/a><\/li>\n<\/ul>\n<hr class=\"wp-block-separator has-alpha-channel-opacity\" \/>\n<h2>Preparation<\/h2>\n<p>This article uses the <a href=\"https:\/\/blog.finxter.com\/python-random-module\/\"><code>random<\/code><\/a> library for each example. For these code samples to run error-free, add the following snippet to the top of each example.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import random<\/pre>\n<hr class=\"wp-block-separator has-alpha-channel-opacity\" \/>\n<h2>Method 1: Use random.choice() and items()<\/h2>\n<p class=\"has-global-color-8-background-color has-background\">This example uses <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/python-random-module\/\" data-type=\"URL\" data-id=\"https:\/\/blog.finxter.com\/python-random-module\/\" target=\"_blank\"><code>random.choice()<\/code><\/a> and <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/python-dict-items-method\/\" data-type=\"URL\" data-id=\"https:\/\/blog.finxter.com\/python-dict-items-method\/\" target=\"_blank\"><code>items()<\/code><\/a> to generate a random <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/python-dictionary\/\" data-type=\"URL\" data-id=\"https:\/\/blog.finxter.com\/python-dictionary\/\" target=\"_blank\">Dictionary<\/a> <em><strong>key:value<\/strong><\/em> pair.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"2\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">el_list = list(els.items())\nrandom_el = random.choice(el_list)\nprint(random_el)<\/pre>\n<p>The above code converts the <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/python-dictionary\/\" data-type=\"URL\" data-id=\"https:\/\/blog.finxter.com\/python-dictionary\/\" target=\"_blank\">Dictionary<\/a> of <a rel=\"noreferrer noopener\" href=\"https:\/\/ptable.com\/?lang=en#Properties\" data-type=\"URL\" data-id=\"https:\/\/ptable.com\/?lang=en#Properties\" target=\"_blank\">Periodic Table<\/a> Elements to a <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/the-ultimate-guide-to-python-tuples\/\" data-type=\"URL\" data-id=\"https:\/\/blog.finxter.com\/the-ultimate-guide-to-python-tuples\/\" target=\"_blank\">List of Tuples<\/a> and saves it to <code>el_list<\/code>. If output to the terminal, the contents of <code>el_list<\/code> contains the following.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">[('Hydrogen', 'H'), ('Helium', 'He'), ('Lithium', 'Li'), ('Beryllium', 'Be'), ('Boron', 'B'), ('Carbon', 'C'), ('Nitrogen', 'N'), ('Oxygen', 'O'), ('Fluorine', 'F'), ('Neon', 'Ne'), ('Sodium', 'Na'), ('Magnesium', 'Mg'), ('Aluminum', 'Al'), ('Silicon', 'Si'), ('Phosphorus', 'P'), ('Sulfur', 'S'), ('Chlorine', 'Cl'), ('Argon', 'Ar'), ('Potassium', 'K'), ('Calcium', 'Ca'), ('Scandium', 'Sc'), ('Titanium', 'Ti'), ('Vanadium', 'V'), ('Chromium', 'Cr'), ('Manganese', 'Mn')]<\/pre>\n<p>Next, <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/python-random-module\/\" data-type=\"URL\" data-id=\"https:\/\/blog.finxter.com\/python-random-module\/\" target=\"_blank\"><code>random.choice()<\/code><\/a> is called and passed one (1) argument: <code>el_list<\/code>. <\/p>\n<p>The results return a random <code><a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/the-ultimate-guide-to-python-tuples\/\" data-type=\"URL\" data-id=\"https:\/\/blog.finxter.com\/the-ultimate-guide-to-python-tuples\/\" target=\"_blank\">Tuple<\/a> <\/code>from the <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/the-ultimate-guide-to-python-tuples\/\" data-type=\"URL\" data-id=\"https:\/\/blog.finxter.com\/the-ultimate-guide-to-python-tuples\/\" target=\"_blank\">List of Tuples<\/a>, saves to <code>random_el <\/code>and is output to the terminal.<\/p>\n<figure class=\"wp-block-table is-style-stripes\">\n<table>\n<tbody>\n<tr>\n<td><code>('Oxygen', 'O')<\/code><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n<p>This code can be streamlined down to the following.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">random_el = random.choice(list(els.items()))<\/pre>\n<figure class=\"wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube\"><a href=\"https:\/\/blog.finxter.com\/how-to-get-a-random-entry-from-a-dictionary\/\"><img decoding=\"async\" src=\"https:\/\/blog.finxter.com\/wp-content\/plugins\/wp-youtube-lyte\/lyteCache.php?origThumbUrl=https%3A%2F%2Fi.ytimg.com%2Fvi%2FLtYPiOZq-BQ%2Fhqdefault.jpg\" alt=\"YouTube Video\"><\/a><figcaption><\/figcaption><\/figure>\n<hr class=\"wp-block-separator has-alpha-channel-opacity\" \/>\n<h2>Method 2: Use random.choice() and keys()<\/h2>\n<p class=\"has-global-color-8-background-color has-background\">This example uses <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/python-random-module\/\" data-type=\"URL\" data-id=\"https:\/\/blog.finxter.com\/python-random-module\/\" target=\"_blank\"><code>random.choice()<\/code><\/a> and <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/python-dict-keys-method\/\" data-type=\"URL\" data-id=\"https:\/\/blog.finxter.com\/python-dict-keys-method\/\" target=\"_blank\"><code>keys()<\/code><\/a> to generate a random <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/python-dictionary\/\" target=\"_blank\">Dictionary<\/a> <em><strong>key<\/strong><\/em>.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"1\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">random_el = random.choice(list(els.keys()))\nprint(random_el)<\/pre>\n<p>The above code calls <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/python-random-module\/\" data-type=\"URL\" data-id=\"https:\/\/blog.finxter.com\/python-random-module\/\" target=\"_blank\"><code>random.choice()<\/code><\/a> and passes it one (1) argument: the keys of the <code>els<\/code> <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/python-dictionary\/\" data-type=\"URL\" data-id=\"https:\/\/blog.finxter.com\/python-dictionary\/\" target=\"_blank\">Dictionary<\/a> converted to a <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/the-ultimate-guide-to-python-tuples\/\" data-type=\"URL\" data-id=\"https:\/\/blog.finxter.com\/the-ultimate-guide-to-python-tuples\/\" target=\"_blank\">List of Tuples<\/a>.<\/p>\n<p>The result returns a random <strong><em>key<\/em><\/strong>, saves to <code>random_el <\/code>and is output to the terminal.<\/p>\n<figure class=\"wp-block-table is-style-stripes\">\n<table>\n<tbody>\n<tr>\n<td><code>Beryllium<\/code><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n<figure class=\"wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube\"><a href=\"https:\/\/blog.finxter.com\/how-to-get-a-random-entry-from-a-dictionary\/\"><img decoding=\"async\" src=\"https:\/\/blog.finxter.com\/wp-content\/plugins\/wp-youtube-lyte\/lyteCache.php?origThumbUrl=https%3A%2F%2Fi.ytimg.com%2Fvi%2FqSEEOOTUrjk%2Fhqdefault.jpg\" alt=\"YouTube Video\"><\/a><figcaption><\/figcaption><\/figure>\n<hr class=\"wp-block-separator has-alpha-channel-opacity\" \/>\n<h2>Method 3: Use random.choice() and dict.values()<\/h2>\n<p class=\"has-global-color-8-background-color has-background\">This example uses <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/python-random-module\/\" data-type=\"URL\" data-id=\"https:\/\/blog.finxter.com\/python-random-module\/\" target=\"_blank\"><code>random.choice()<\/code><\/a> and <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/python-dictionary-values-method\/\" data-type=\"URL\" data-id=\"https:\/\/blog.finxter.com\/python-dictionary-values-method\/\" target=\"_blank\"><code>values()<\/code><\/a> to generate a random <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/python-dictionary\/\" target=\"_blank\">Dictionary<\/a> <em><strong>value<\/strong><\/em>.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"1\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">random_el = random.choice(list(els.values()))\nprint(random_el)<\/pre>\n<p>The above code calls <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/python-random-module\/\" data-type=\"URL\" data-id=\"https:\/\/blog.finxter.com\/python-random-module\/\" target=\"_blank\"><code>random.choice()<\/code><\/a> and passes it one (1) argument: the keys of the <code>els<\/code> <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/python-dictionary\/\" data-type=\"URL\" data-id=\"https:\/\/blog.finxter.com\/python-dictionary\/\" target=\"_blank\">Dictionary<\/a> converted to a <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/the-ultimate-guide-to-python-tuples\/\" data-type=\"URL\" data-id=\"https:\/\/blog.finxter.com\/the-ultimate-guide-to-python-tuples\/\" target=\"_blank\"><code>List of Tuples<\/code><\/a>.<\/p>\n<p>The result returns a random <strong><em>value<\/em><\/strong>, saves to <code>random_el <\/code>and is output to the terminal.<\/p>\n<figure class=\"wp-block-table is-style-stripes\">\n<table>\n<tbody>\n<tr>\n<td><code>Si<\/code><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n<figure class=\"wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube\"><a href=\"https:\/\/blog.finxter.com\/how-to-get-a-random-entry-from-a-dictionary\/\"><img decoding=\"async\" src=\"https:\/\/blog.finxter.com\/wp-content\/plugins\/wp-youtube-lyte\/lyteCache.php?origThumbUrl=https%3A%2F%2Fi.ytimg.com%2Fvi%2FENjl8yKu00M%2Fhqdefault.jpg\" alt=\"YouTube Video\"><\/a><figcaption><\/figcaption><\/figure>\n<hr class=\"wp-block-separator has-alpha-channel-opacity\" \/>\n<h2>Method 4: Use sample()<\/h2>\n<p class=\"has-global-color-8-background-color has-background\">This example uses the <a rel=\"noreferrer noopener\" href=\"https:\/\/docs.python.org\/3\/library\/random.html\" data-type=\"URL\" data-id=\"https:\/\/docs.python.org\/3\/library\/random.html\" target=\"_blank\"><code>sample()<\/code><\/a> function to generate a random <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/python-dictionary\/\" target=\"_blank\">Dictionary<\/a> <em><strong>key<\/strong><\/em>.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">from random import sample\nrandom_el = sample(list(els), 1)\nprint(random_el)<\/pre>\n<p>The above code requires <a rel=\"noreferrer noopener\" href=\"https:\/\/docs.python.org\/3\/library\/random.html\" data-type=\"URL\" data-id=\"https:\/\/docs.python.org\/3\/library\/random.html\" target=\"_blank\"><code>sample<\/code><\/a> to be imported from the <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/python-random-module\/\" data-type=\"URL\" data-id=\"https:\/\/blog.finxter.com\/python-random-module\/\" target=\"_blank\"><code>random<\/code><\/a> library.<\/p>\n<p>Then, <a rel=\"noreferrer noopener\" href=\"https:\/\/docs.python.org\/3\/library\/random.html\" data-type=\"URL\" data-id=\"https:\/\/docs.python.org\/3\/library\/random.html\" target=\"_blank\"><code>sample()<\/code><\/a> is called and passed two (2) arguments: <code>els<\/code> converted to a <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/the-ultimate-guide-to-python-tuples\/\" data-type=\"URL\" data-id=\"https:\/\/blog.finxter.com\/the-ultimate-guide-to-python-tuples\/\" target=\"_blank\"><code>List of Tuples<\/code><\/a> and the number of random <strong><em>keys<\/em><\/strong> to return. <\/p>\n<p>The results save to <code>random_el<\/code> and is output to the terminal.<\/p>\n<figure class=\"wp-block-table is-style-stripes\">\n<table>\n<tbody>\n<tr>\n<td><code>['Carbon']<\/code><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n<hr class=\"wp-block-separator has-alpha-channel-opacity\" \/>\n<h2>Method 5: Use np.random.choice()<\/h2>\n<p class=\"has-global-color-8-background-color has-background\">This example uses NumPy and <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/python-random-seed-a-deep-dive\/\" data-type=\"URL\" data-id=\"https:\/\/blog.finxter.com\/python-random-seed-a-deep-dive\/\" target=\"_blank\"><code>np.random.choice()<\/code><\/a> to generate a random <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/python-dictionary\/\" target=\"_blank\">Dictionary<\/a> <em><strong>key<\/strong><\/em>.<\/p>\n<p>Before moving forward, please ensure the <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/numpy-tutorial\/\" data-type=\"URL\" data-id=\"https:\/\/blog.finxter.com\/numpy-tutorial\/\" target=\"_blank\">NumPy<\/a> library is installed. Click <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/how-to-install-numpy-in-python\/\" data-type=\"URL\" data-id=\"https:\/\/blog.finxter.com\/how-to-install-numpy-in-python\/\" target=\"_blank\">here<\/a> if you require instructions.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import numpy as np random_el = np.random.choice(list(els), 1)\nprint(random_el)<\/pre>\n<p>This code imports the <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/numpy-tutorial\/\" data-type=\"URL\" data-id=\"https:\/\/blog.finxter.com\/numpy-tutorial\/\" target=\"_blank\">NumPy<\/a> library installed above.<\/p>\n<p>Then, <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/python-random-seed-a-deep-dive\/\" data-type=\"URL\" data-id=\"https:\/\/blog.finxter.com\/python-random-seed-a-deep-dive\/\" target=\"_blank\"><code>np.random.choice()<\/code><\/a> is called and passed two (2) arguments: <code>els<\/code> converted to a <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/the-ultimate-guide-to-python-tuples\/\" data-type=\"URL\" data-id=\"https:\/\/blog.finxter.com\/the-ultimate-guide-to-python-tuples\/\" target=\"_blank\">List of Tuples<\/a> and the number of random <strong><em>keys<\/em><\/strong> to return. <\/p>\n<p>The results save to <code>random_el<\/code> and is output to the terminal.<\/p>\n<figure class=\"wp-block-table is-style-stripes\">\n<table>\n<tbody>\n<tr>\n<td><code>['Chromium' 'Silicon' 'Oxygen']<\/code><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n<p class=\"has-global-color-8-background-color has-background\"><img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f4a1.png\" alt=\"\ud83d\udca1\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/><strong>Note<\/strong>: <a rel=\"noreferrer noopener\" href=\"https:\/\/docs.python.org\/3\/library\/random.html\" data-type=\"URL\" data-id=\"https:\/\/docs.python.org\/3\/library\/random.html\" target=\"_blank\"><\/a><a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/python-random-seed-a-deep-dive\/\" data-type=\"URL\" data-id=\"https:\/\/blog.finxter.com\/python-random-seed-a-deep-dive\/\" target=\"_blank\"><code>np.random.choice()<\/code><\/a> has an additional parameter that can be passed. This parameter is a <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/python-lists\/\" data-type=\"URL\" data-id=\"https:\/\/blog.finxter.com\/python-lists\/\" target=\"_blank\"><code>List<\/code><\/a> containing associated probabilities.<\/p>\n<hr class=\"wp-block-separator has-alpha-channel-opacity\" \/>\n<h2>Bonus:<\/h2>\n<p class=\"has-global-color-8-background-color has-background\">This code generates a random <em>key:value<\/em> pair from a list of tuples. When the teacher runs this code, a random question displays on the screen and waits for a student to answer. Press 1 to display the answer, 2 to quit.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import keyboard\nimport random\nimport time els = {'Hydrogen': 'H', 'Helium': 'He', 'Lithium': 'Li', 'Beryllium': 'Be', 'Boron': 'B', 'Carbon': 'C', 'Nitrogen': 'N', 'Oxygen': 'O', 'Fluorine': 'F', 'Neon': 'Ne', 'Sodium': 'Na', 'Magnesium': 'Mg', 'Aluminum': 'Al', 'Silicon': 'Si', 'Phosphorus': 'P', 'Sulfur': 'S', 'Chlorine': 'Cl', 'Argon': 'Ar', 'Potassium': 'K', 'Calcium': 'Ca', 'Scandium': 'Sc', 'Titanium': 'Ti', 'Vanadium': 'V', 'Chromium': 'Cr', 'Manganese': 'Mn'} print('1 Answer 2 quit')\ndef quiz(): while True: k, v = random.choice(list(els.items())) print(f'\\nWhat is the Symbol for {k}?') pressed = keyboard.read_key() if pressed == '1': print(f'The answer is {v}!') elif pressed == '2': print(\"Exiting\\n\") exit(0) time.sleep(5)\nquiz()<\/pre>\n<p class=\"has-text-align-center has-base-3-color has-global-color-9-background-color has-text-color has-background\"><img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/2728.png\" alt=\"\u2728\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/>Finxter Challenge!<br \/>Write code to allow the teacher to enter the answer!<\/p>\n<\/p>\n<hr class=\"wp-block-separator has-alpha-channel-opacity\" \/>\n<h2>Summary<\/h2>\n<p>This article has provided five (5) ways to get a random entry from a Dictionary to select the best fit for your coding requirements.<\/p>\n<p>Good Luck &amp; Happy Coding!<\/p>\n<hr class=\"wp-block-separator has-alpha-channel-opacity\" \/>\n<h2>Programmer Humor &#8211; Blockchain<\/h2>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"280\" height=\"394\" src=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2022\/07\/image-31.png\" alt=\"\" class=\"wp-image-457795\" srcset=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2022\/07\/image-31.png 280w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2022\/07\/image-31-213x300.png 213w\" sizes=\"auto, (max-width: 280px) 100vw, 280px\" \/><figcaption><em>&#8220;Blockchains are like grappling hooks, in that it&#8217;s extremely cool when you encounter a problem for which they&#8217;re the right solution, but it happens way too rarely in real life.&#8221;<\/em> <strong>source <\/strong> &#8211; <a href=\"https:\/\/imgs.xkcd.com\/comics\/blockchain.png\" data-type=\"URL\" data-id=\"https:\/\/imgs.xkcd.com\/comics\/blockchain.png\" target=\"_blank\" rel=\"noreferrer noopener\">xkcd<\/a><\/figcaption><\/figure>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>5\/5 &#8211; (1 vote) Problem Formulation and Solution Overview This article will show you how to get a random entry from a Dictionary in Python. To make it more interesting, we have the following running scenario: The Plot: Mr. Sinclair, an 8th great Science Teacher, is giving his students a quiz on the first 25 [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[857],"tags":[73,468,528],"class_list":["post-128068","post","type-post","status-publish","format-standard","hentry","category-python-tut","tag-programming","tag-python","tag-tutorial"],"_links":{"self":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/128068","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/comments?post=128068"}],"version-history":[{"count":0,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/128068\/revisions"}],"wp:attachment":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/media?parent=128068"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/categories?post=128068"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/tags?post=128068"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}